简体   繁体   English

从IDEA运行main时发生FileNotFoundException:file:xxx \\ .m2 \\ repository \\ xxxx \\ xxx \\ xxx.jar!\\ filename

[英]FileNotFoundException:file:xxx\.m2\repository\xxxx\xxx\xxx.jar!\filename, when running main from IDEA

I have a project that build from eclplse, and a java class that main method runs well but fail to run when I try to run it from IDEA. 我有一个从eclplse构建的项目,还有一个java类,该类的main方法可以很好地运行,但是当我尝试从IDEA中运行它时却无法运行。

The main method will initially try to load a properties file from classbath by below code from ANOTHER dependency project(wemq-client): main方法最初将尝试通过ANOTHER依赖项目(wemq-client)的以下代码从classbath加载属性文件:

 filePath = this.getClass().getClassLoader().getResource(fileName).getPath();
 ....//something else

 prop = new Properties();
 prop.load(new FileInputStream(new File(filePath)));

But when I run in InteliJ, I have following error: 但是当我在InteliJ中运行时,出现以下错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at .....
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: file:\C:\Users\jaskeylin\.m2\repository\cn\webank\wemq\wemq-client\0.1.3\wemq-client-0.1.3.jar!\wemq-client.properties (文件名、目录名或卷标语法不正确。)

which 文件名、目录名或卷标语法不正确。 means file name or dictionary name syntax not correct. 表示文件名、目录名或卷标语法不正确。语法不正确。

We can see from the error statck that it tries to find this file from maven repo's package jar file. 从错误statck可以看出,它试图从maven repo的package jar文件中找到该文件。 Why is that? 这是为什么? How can I fix it. 我该如何解决。

PS: If you are suggesting I modify the code, please also explain why this works from eclipse, since this works quite fine from my teamates, there is only me who hopes to use IDEA PS:如果您建议我修改代码,请也解释一下为什么它在eclipse上有效,因为在我的团队中效果很好,所以只有我希望使用IDEA

As you can see from the file name in the error message, you're trying to open file wemq-client.properties inside file wemq-client-0.1.3.jar using a FileInputStream . 从错误消息中的文件名可以看出,您正在尝试使用FileInputStream在文件wemq-client-0.1.3.jar打开文件wemq-client.properties You cannot do that. 你不能这样做。 You can only open real files on the file system, not files inside archives. 您只能在文件系统上打开实际文件,而不能在存档中打开文件。

In short, NEVER EVER do getResource(...).getPath() . 简而言之, 不做getResource(...).getPath() There is a reason the getResource(String) method returns a URL , and not a File . 原因是getResource(String)方法返回URL而不是File Because the resource may not be a file. 由于资源可能不是一个文件。

Instead, use the getResourceAsStream(String) , and give that InputStream directly to the load(InputStream) method. 而是使用getResourceAsStream(String) ,并将该InputStream直接提供给load(InputStream)方法。

As always, remember to close your resources, preferably using try-with-resources. 与往常一样,请记住关闭资源,最好使用try-with-resources。

prop = new Properties();
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName)) {
    prop.load(inputStream);
}

Or use any alternate version of getResourceAsStream() as suggested in answer by @SanjitKumarMishra . 或使用@SanjitKumarMishra的答案中建议的任何替代版本的getResourceAsStream()

You can use the getResource(String) method if you want, but you must then open the stream using the URL.openStream() method. 如果需要, 可以使用getResource(String)方法,但随后必须使用URL.openStream()方法打开流。

URL fileUrl = this.getClass().getClassLoader().getResource(fileName);
....//something else

prop = new Properties();
try (InputStream inputStream = fileUrl.openStream()) {
    prop.load(inputStream);
}

The reason it works inside Eclipse, is that you're running directly off files on the file system, however when you package and deploy your code, it's packaged inside jar files, and you get the error you see. 它在Eclipse中运行的原因是,您直接在文件系统上的文件之外运行,但是在打包和部署代码时,它打包在jar文件中,您会看到错误。

you cannot using FileInputStream to read files in jar, you can using someclass.class.getResourceAsStream(name) to read the file 您不能使用FileInputStream读取jar中的文件,可以使用someclass.class.getResourceAsStream(name)读取文件

just 只是

 prop = new Properties();
 prop.load(this.getClass().getResourceAsStream(fileName));

If you are loading the class from an Application Server, so your should use Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) instead of this.getClass().getClassLoader().getResourceAsStream(fileName) . 如果要从Application Server加载类,则应使用Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName)而不是this.getClass().getClassLoader().getResourceAsStream(fileName) this.getClass().getResourceAsStream() will also work. this.getClass().getResourceAsStream()也将起作用。

Read this article for more detailed information about that particular problem. 阅读本文以获得有关该特定问题的更多详细信息。

You can use HTTPSERVLETREQUEST to get file path. 您可以使用HTTPSERVLETREQUEST来获取文件路径。

HttpServletRequest request = ServletActionContext.getRequest(); HttpServletRequest请求= ServletActionContext.getRequest();

String filePath = request.getSession().getServletContext().getRealPath("/")+filename; 字符串filePath = request.getSession()。getServletContext()。getRealPath(“ /”)+ filename;

暂无
暂无

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

相关问题 缺少Codebase清单属性:xxx.jar - Missing Codebase manifest attribute for:xxx.jar xxx.jar中没有主清单属性[maven-assembly-plugin不起作用] - no main manifest attribute, in xxx.jar [maven-assembly-plugin is not working] 如何在类路径上运行带有额外文件的Spring Boot应用程序而不修改应用程序(xxx.jar) - how to run Spring Boot application with an extra file on classpath without modifying the application (the xxx.jar) 线程“main”中的异常 java.lang.IllegalArgumentException:没有为 URI“http://xxx.xxx.xxx.xxx:xxxx”的方案“http”安装 NetworkModule - Exception in thread “main” java.lang.IllegalArgumentException: no NetworkModule installed for scheme “http” of URI “http://xxx.xxx.xxx.xxx:xxxx” 添加失败:stat /var/lib/docker/tmp/docker-builder673702145/target/xxx.jar:没有这样的文件或目录 - ADD failed: stat /var/lib/docker/tmp/docker-builder673702145/target/xxx.jar: no such file or directory `java -Dspring.profiles.active -jar xxx.jar` 和 `java -jar xxx.jar --spring.profiles.active` 有什么区别 - What is the difference between `java -Dspring.profiles.active -jar xxx.jar` and `java -jar xxx.jar --spring.profiles.active` 是什么导致警告“类路径条目/XXX/xxx.jar将不会导出或发布。 运行时可能会导致ClassNotFoundException。” - What causes the warning “Classpath entry /XXX/xxx.jar will not be exported or published. Runtime ClassNotFoundException may result.” java -jar xxx.jar在Mac osx和Windows上工作得很好,但我收到以下错误:“错误:无法在raspberry pi上找到或加载主类” - java -jar xxx.jar working very well on Mac osx and Windows but I get the following error: “Error: Could not find or load main class” on raspberry pi “mvn spring-boot:run”vs“java jar target / xxx.jar” - “mvn spring-boot:run” vs “java jar target/xxx.jar” 无法使用springboot通过java连接到elasticsearch,说找不到xxx.jar - Can't connect to elasticsearch by java with springboot, say can not find xxx.jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM