MyBatis 加载 XML:java.io.IOException:找不到资源(eclipse)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22176966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MyBatis loading XML : java.io.IOException: Could not find resource (eclipse)
提问by libik
I found strange behaviour while using MyBatis with Eclipse.
我在 Eclipse 中使用 MyBatis 时发现了奇怪的行为。
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 :
MyBatis 很难找到我的 xml,所以我尝试使用绝对路径(仅用于测试目的),但它仍然抛出错误:
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?
有任何想法吗?
采纳答案by Karthik Prasad
You have 2 scenarios
你有 2 个场景
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);
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 {}
使用 java io 读取文件。因为你给出了绝对路径。如果文件存在,它将始终读取文件。
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);
您尝试读取配置文件的场景是使用资源(我相信这是您的主要目标)。资源类总是尝试从类路径读取文件。这是来自 Resources.java 类的 java 注释作为证明。
/* * 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 thisFrom 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
现在如何解决这个问题从文件夹结构我假设您正在使用 Maven。因此,默认情况下,您的类路径将是SpringBatis/src/main/java
或SpringBatis/src/main/resources
因此您可以将资源路径提供为com/mkyong/MyBatis/xml/batisConfig.xml
,在您的情况下,当前资源路径是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.
如果您使用的是 eclipse,您可以按如下方式验证类路径。就在项目-> 项目属性-> java 构建路径-> 源选项卡上。您应该找到所有相关的类路径。例如,对于您的项目 SpringBatis,您会发现 SpringBatis/main/java 包含包含文件、排除文件、输出目录等。
NoteSince 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)
注意由于您已将 batisConfig.xml 放在 src/main/java 下。默认情况下,maven 配置为仅包含*/*.java
文件(仅 java 文件),如果需要包含您需要相应配置的所有文件。但我建议将 xml 文件移动到 src/main/resources 目录(这又是一个类路径)