-2

I'm testing a web application with Eclipse + Tomcat, Eclipse deploys the web application files and launches Tomcat, and the application runs fine. But when MyBatis is trying to open it's XML configuration files, it looks for them in

C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\persistence\db\oracle.xml

instead of the correct place:

C:\workspace\mywebapp\src\persistence\db\oracle.xml

Where is MyBatis supposed to look for XML files?

EDIT:

This is where I specify the relative path:

String cfgFile = "persistence/db/oracle.xml";
Reader reader  = Resources.getResourceAsReader(cfgFile);
session.put(db, new SqlSessionFactoryBuilder().build(reader));
golimar
  • 2,419
  • 1
  • 22
  • 33

2 Answers2

1

Resources.getResourceAsReader looks files in classpath. For web application running in tomcat classpath consist of WEB-INF/classes and all jars from WEB-INF/lib and tomcat folders like $TOMCAT_HOME\lib.

The issue you encounter most probably is caused by the fact that oracle.xml file is not added to deployment. It looks like c:\workspace\myweapp\src is not among source folder of eclipse project so eclipse doesn't copy files from it to the folder which is deployed to tomcat. Depending on your existing project structure you may need to create subfolder in src and add persistence with all subfolders there. This will allow you to avoid clash if some subfolder of src is already a source folder in eclipse. I would recommend to use maven project structure:

src
   main
      * java  
          you java source code here organized by package
      * resources
          persistence

I marked folders which should be added as source folder to eclipse with *.

Please note that it is not correct to say that C:\workspace\mywebapp\src\persistence\db\oracle.xml is a correct place to search for it. After you create a war to deploy it on production this path most probably will not be available at your production server. What you really need is to include persistence\db\oracle.xml to the war in appropriate place (under WEB-INF/classes).

  • Analyzing your answer I realized that there was a uppercase/lowercase problem in my cfgFile variable. Such a problem with Java source files is detected by Eclipse and it shows the red error icon, but not with XML files as in this case – golimar Sep 18 '14 at 12:17
0

Maybe you need another class loader 1. Try this:

String cfgFile = "persistence/db/oracle.xml";
ClassLoader classloader = Thread.currentThread().getContextClassLoader()
Reader reader  = Resources.getResourceAsReader(classloader, cfgFile);

Notes

  1. See Difference between thread's context class loader and normal classloader and you may want to see the code of org.apache.ibatis.io.Resources. You find it here.
Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148