简体   繁体   English

使用getClassLoader()。getResource时,Linux机器上的FileNotFoundException

[英]FileNotFoundException on linux machine when using getClassLoader().getResource

I made already quite some research on internet for this problem. 对于这个问题,我已经在互联网上进行了一些研究。 I had no luck so far. 到目前为止我还没有运气。 Basically this piece of code works fine on Windows with my Junit test src\\test\\java\\com\\project\\utils\\MyTestCase.java : 基本上,这段代码在Windows上可以通过Junit测试src \\ test \\ java \\ com \\ project \\ utils \\ MyTestCase.java正常运行:

URL urlApplicationContext = this.getClass().getClassLoader().getResource("applicationContext.xml");
final String[] paths = { urlApplicationContext.getFile()};
ApplicationContext ctx = new FileSystemXmlApplicationContext(paths);

This file is located there: 该文件位于:

\\src\\test\\resources\\applicationContext.xml \\ SRC \\测试\\资源\\ applicationContext.xml中

However on the Jenkins machine which run on linux I got the following error : 但是在Linux上运行的Jenkins机器上,出现以下错误:

testSimple(com.project.ClientImplTest): IOException parsing XML document from file [/data/continuous/workspace/sonar/main_proj/data/continuous/workspace/sonar/main_proj/target/main/WEB-INF/test-classes/applicationContext.xml]; testSimple(com.project.ClientImplTest):IOException从文件[/ data / continuous / workspace / sonar / main_proj / data / continuous / workspace / sonar / main_proj / target / main / WEB-INF / test-classes / applicationContext解析XML文档.XML]; nested exception is java.io.FileNotFoundException: data/continuous/workspace/sonar/main_proj/target/main/WEB-INF/test-classes/applicationContext.xml (No such file or directory) 嵌套的异常是java.io.FileNotFoundException:data / continuous / workspace / sonar / main_proj / target / main / WEB-INF / test-classes / applicationContext.xml(无此类文件或目录)

I already verified that the file /data/continuous/workspace/sonar/main_proj/target/main/WEB-INF/test-classes/applicationContext.xml does exist. 我已经确认文件/data/continuous/workspace/sonar/main_proj/target/main/WEB-INF/test-classes/applicationContext.xml确实存在。

Why the getResource() does not find the correct path on Linux. 为什么getResource()在Linux上找不到正确的路径。 It seems it finds data/continous/... instead of /data/continous/... for some reason ? 似乎由于某种原因找到了data / continuous / ...而不是/ data / continent /...。 Therefore FileSystemXmlApplicationContext may return an exception because it cannot find the file. 因此,FileSystemXmlApplicationContext可能会返回异常,因为它找不到文件。

Thanks 谢谢

I had the same problem in my project - it was caused by spaces in the path - to handle such cases, you need to use the URL's toURI() method - do following: 我在项目中遇到了同样的问题-它是由路径中的空格引起的-要处理此类情况,您需要使用URL的toURI()方法-请执行以下操作:

ApplicationContext ctx;
URL urlApplicationContext = this.getClass().getClassLoader().getResource("applicationContext.xml");
if (urlApplicationContext != null) {
    File appCtxFile = new File(urlApplicationContext.toURI());
    ctx = new FileSystemXmlApplicationContext(new String[]{ appCtxFile.getAbsolutePath() });
} else {
    throw new RuntimeException("Cannot find XML file 'applicationContext.xml'");
}

Try to add some debug output. 尝试添加一些调试输出。 URL has nice toString() method. URL具有不错的toString()方法。 Thus you'll get where app is looking for file. 因此,您将获得应用程序正在寻找文件的位置。

It looks like you missed slash at the very beginning. 您似乎一开始就错过了斜线。 Replace data/continuous/... to /data/continuous/... in your resource loader. 在资源加载器中将data/continuous/...替换为/data/continuous/...

Since I don't have enough rep to upvote or comment or anything I will say what I need to say here lol 由于我没有足够的代表来发表评论或发表评论或其他内容,因此我会在这里说些什么

Oifan is absolutely right (read answer needs accepting). Oifan是完全正确的(阅读答案需要接受)。

Had the same issue on Jenkins. 在詹金斯身上也有同样的问题。 Code was trying to access a file during a test, path had spaces in it, resulted in FileNotFoundException . 测试期间,代码试图访问文件,路径中包含空格,从而导致FileNotFoundException The file was definitely in the right location. 该文件肯定在正确的位置。

My testing also showed that the spaces were the culprit. 我的测试还表明,这些空间是罪魁祸首。 We were just going to rename the project in Jenkins which would have resulted in no more spaces in our directory structure. 我们只打算在Jenkins中重命名该项目,这将导致目录结构中没有更多空间。 Then I came across Oifan's answer. 然后我遇到了奥凡的答案。

Converting the URL from getResource() to a URI and passing that to File() solved the problem for us too. URLgetResource()转换为URI并将其传递给File()也为我们解决了这个问题。

BTW, the problem is not isolated to linux. 顺便说一句,这个问题不是孤立的Linux。 I have seen this behavior on Windows Jenkins slaves as well. 我也已经在Windows Jenkins从属服务器上看到了此行为。

实际上,詹金斯并没有抱怨applicationContext.xml文件,而是以下文件: data/continuous/workspace/sonar/main_proj/target/main/WEB-INF/test-classes/applicationContext-hibernate-junit.xml

Why the getResource() does not find the correct path on Linux. 为什么getResource()在Linux上找不到正确的路径。

I think you need to look at your code and stacktraces again. 我认为您需要再次查看您的代码和stacktraces。

The getResource() method does not throw IOException , or any subclasses of IOException . getResource()方法不抛出IOException ,或任何子类IOException Not ever. 永远不会。

The exception you are seeing is being thrown by ... something else. 您所看到的异常是由...引发的。 And it is pretty clear from the message that the code that throws that exception is not looking for a file called "applicationContext.xml" either. 从消息中可以很明显地看出,引发该异常的代码也不在寻找名为“ applicationContext.xml”的文件。

UPDATE UPDATE

Even following your correction, it remains an incontravertable fact that getResource() cannot cause a FileNotFoundException . 即使进行了更正, getResource()也不会导致FileNotFoundException仍然是一个不可逆转的事实。 You are looking at the wrong code. 您正在查看错误的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 getClassLoader()。getResource()抛出NullPointerException?为什么不FileNotFoundException? - getClassLoader().getResource() throwing NullPointerException? Why not FileNotFoundException? getClassLoader()。getResource()和斜杠“ /” - getClassLoader().getResource() and slash '/' getClassLoader().getResource() 返回 null - getClassLoader().getResource() returns null 使用.getClassLoader()从目录加载所有文件.getResource() - Load all files from directory using .getClassLoader().getResource() this.getClass()。getClassLoader()。getResource()==异常 - this.getClass().getClassLoader().getResource() == Exception 包含getClassLoader()。getResource()的方法的Junit - Junit for a method containing getClassLoader().getResource() 即使路径正确,getClass()。getClassLoader()。getResource(path)始终返回null - getClass().getClassLoader().getResource(path) always return null, even when the path is correct 从jar throgh class.getClassLoader()。getResource(“”);运行时,在Maven项目Java中找到一个文件夹。 方法 - Locate a folder in a maven project java when running from jar throgh class.getClassLoader().getResource(“”); method 使用 getClassLoader().getResource() 加载 mmdb 文件会返回 null 尽管该文件存在于资源中 - loading mmdb file using getClassLoader().getResource() returns null although the file exist in the resources getClassLoader()。getResource()。getPath()未返回预期结果 - getClassLoader().getResource().getPath() not returning expected result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM