Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read configuration in a modular Java project. #2285

Closed
diziaq opened this issue Jul 9, 2021 · 4 comments
Closed

Cannot read configuration in a modular Java project. #2285

diziaq opened this issue Jul 9, 2021 · 4 comments

Comments

@diziaq
Copy link

diziaq commented Jul 9, 2021

MyBatis version

3.5.7

Database vendor and version

any

Test case or example project

Minimal Maven project

src
  main
     java
        mypackage
            Main.java
        module-info.java // just basic module declaration
     resources
        config
           valid-mybatis-config.xml

Steps to reproduce

public class Main{
  public static void main(String[] args) {
    try{
       Reader r = Resources.getResourceAsReader("config/valid-mybatis-config.xml");
       print("reads the resource when file "module-info.java" is absent");
    } catch (IOException e){
       print("throws and comes here when runs in java module, i.e. "module-info.java" is present");
    }
  }   
}

Expected result

Reads configuration files of myBatis from "resources"

Actual result

Fails with IOException and causes other exceptions related to reading XML resources (config and mappers).

java.io.IOException: Could not find resource  config/valid-mybatis-config.xml
	at org.mybatis@3.5.7/org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
	at org.mybatis@3.5.7/org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)
	at org.mybatis@3.5.7/org.apache.ibatis.io.Resources.getResourceAsReader(Resources.java:160)
	at my.sample.module/my.sample.module.mypackage.Main.main(Main.java:11)

Hint

Resources.getResourceAsReader relies on ClassLoader.getResourceAsStream, which cannot find resources in module. Please, consider using Class.getResourceAsStream and Module.getResourceAsStream or provide an API so that user could read XML resources in a named module.

@harawata
Copy link
Member

harawata commented Jul 9, 2021

Hello @diziaq ,

Please try the latest version 3.5.7 first.
There was a fix that might be related ( #1974 ).

If it still didn't work, please post the full stack trace.

@diziaq
Copy link
Author

diziaq commented Jul 13, 2021

Thank you @harawata. Tried 3.5.7 and updated the description with full stack trace.
Have checked code in org.apache.ibatis.io.Resources: no significant between 3.5.4 and 3.5.7

@harawata
Copy link
Member

@diziaq ,

It should work if the config package is fully opened i.e. there should be an entry like the below in your module-info.java.

opens config;

Note to self : related to #1347

@diziaq
Copy link
Author

diziaq commented Jul 20, 2021

@harawata You are right. Thanks a lot. Really appreciate you help.

@diziaq diziaq closed this as completed Jul 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants