[英]Trouble building executable jar in IntelliJ
I'm trying to export my project to a jar file in IntelliJ using these instructions , and failing. 我试图使用这些说明将项目导出到IntelliJ中的jar文件,但失败了。
I'm getting a NullPointerException
on this line in my application: 我在应用程序的这一行上收到NullPointerException
:
ClassLoader.getSystemClassLoader().getResource(".").getPath();
Note that this same project exports fine in Eclipse. 注意,该项目在Eclipse中可以很好地导出。
I've tried changing the Class-Path
in the manifest to .
我尝试将清单中的Class-Path
更改为.
, as I've noticed this change in the jar exported by Eclipse. ,因为我已经注意到Eclipse导出的jar中的这一更改。
Then, my application gets a bit further, but bails when trying to load any of the resource files included in my jar. 然后,我的应用程序进行了进一步扩展,但是在尝试加载jar中包含的任何资源文件时失败了。 This is how: 这是这样的:
Assume my jar is placed in 假设我的罐子放在
/Users/me/screwed.jar
When my application tries to open resource.file
in the root of the jar file, it's searching for it here: 当我的应用程序尝试在jar文件的根目录中打开resource.file
时,将在此处进行搜索:
/Users/me/resource.file
This triggers a NullPointerException
. 这将触发NullPointerException
。
How do I fix this, team? 小组,我该如何解决?
Use this line to load your path: 使用此行加载路径:
this.getClass().getClassLoader().getResource(".").getPath();
This should be the root of your jar file and allow you to open resources within it. 这应该是jar文件的根目录,并允许您在其中打开资源。
For static context, use 对于静态上下文,请使用
MyStaticClassName.class.getClassLoader().getResource(".").getPath();
where MyStaticClassName is some staic utility class. 其中MyStaticClassName是一些staic实用程序类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.