Question

I found strange behaviour while using MyBatis with Eclipse.

I have difficulty with MyBatis with finding my xml, so I tried to use absolute path (just for testing purposes) and it still throw error :

To be sure that file exists, I added check for file existence before using it as Resource, so I am sure that this file exists :

        String resource = "e:/prace/workspace/SpringBatis/src/main/java/com/mkyong/MyBatis/xml/batisConfig.xml";// path of the mybatis configuration file.
        File file = new File(resource);
        System.out.println(file.exists());
        Reader reader = Resources.getResourceAsReader(resource);// read the mybatis confiuguration xml file 

Having this output :

true
java.io.IOException: Could not find resource e:/prace/workspace/SpringBatis/src/main/java/com/mkyong/MyBatis/xml/batisConfig.xml
    at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:89)

Any ideas?

Was it helpful?

Solution

You have 2 scenarios

  1. Reading the file using java io. Since You had given absolute path. It will always read the file if it exists.

        String resource = "e:/prace/workspace/SpringBatis/src/main/java/com/mkyong/MyBatis/xml/batisConfig.xml";// path of the mybatis configuration file.
        File file = new File(resource);
        System.out.println(file.exists());
        Reader reader = new FileReader(resource);
    
  2. The scenario where you are trying to read config file is using Resources (which is your primary goal I believe). Resources class always tries to read file from class path. Here is java comment from the Resources.java class as a proof.

    /*
      * Gets a URL as a Reader
      *
      *  @param urlString - the URL to get
      * @return A Reader with the data from the URL
      * @throws java.io.IOException If the resource cannot be found or read
    */
    public static Reader getUrlAsReader(String urlString) throws IOException {}
    

Now how to resolve this From the folder structure I assume you are using the Maven. Hence by default your classpath would be SpringBatis/src/main/java or SpringBatis/src/main/resources Hence you can provide the resource path as com/mkyong/MyBatis/xml/batisConfig.xml, in your case currently Resources path turns out be e:/prace/workspace/SpringBatis/src/main/java/e:/prace/workspace/SpringBatis/src/main/java/com/mkyong/MyBatis/xml/batisConfig.xml

Thus your code would should look like.

 String resource = "com/mkyong/MyBatis/xml/batisConfig.xml";// path of the mybatis configuration file.
               // File file = new File(resource);
                System.out.println(file.exists());
               // Reader reader = new FileReader(resource);

If you are using eclipse you can verify the classpath as follows. Right on project->project properties-> java build path -> sources tab. You should find all th relevant classpaths. For example for your project SpringBatis you would find SpringBatis/main/java with included files, excluded files, out directory etc.

Note Since you have placed batisConfig.xml under src/main/java. By default maven configures to include only */*.java file( only java files) if need to include all the files you need to configure accordingly. But I would recommend to move the xml file to src/main/resources directory( which is again a classpath)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top