可执行 jar 找不到属性文件
我在程序中使用此代码来加载属性文件:
Properties properties = new Properties();
URL url = new App().getClass().getResource(PROPERTIES_FILE);
properties.load(url.openStream());
该代码在 Eclipse 中运行良好。然后我将程序打包到名为 MyProgram.jar 的 JAR 中,并运行它,我在第二行收到 NullPointerException。 JAR 不包含属性文件,它们位于同一目录中。我正在使用 Maven 创建 JAR。我该如何解决这个问题?
更新:我不想将属性文件添加到 JAR,因为它将在部署时创建。
I use this code in my program to load a properties file:
Properties properties = new Properties();
URL url = new App().getClass().getResource(PROPERTIES_FILE);
properties.load(url.openStream());
The code runs fine in Eclipse. Then I package the program into a JAR named MyProgram.jar, and run it, I got a NullPointerException at the second line. The JAR doesn't contain the properties file, they both are in the same directory. I am using Maven to create the JAR. How can I fix this problem?
UPDATE: I don't want to add the properties file to the JAR, since it will be created at deployment time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
BalusC 是对的,您需要指示 Maven 在
Class-Path:
条目中生成一个包含当前目录(.
)的MANIFEST.MF
。假设您仍在使用 Maven 程序集插件和
jar-with-dependencies
描述符来构建可执行 JAR,您可以使用以下命令告诉插件这样做:BalusC is right, you need to instruct Maven to generate a
MANIFEST.MF
with the current directory (.
) in theClass-Path:
entry.Assuming you're still using the Maven Assembly Plugin and the
jar-with-dependencies
descriptor to build your executable JAR, you can tell the plugin to do so using the following:有两种解决方法:
不要将 JAR 用作可执行 JAR,而是用作库。
获取 JAR 文件的根位置并从中获取属性文件。
两者都不是推荐的方法!推荐的方法是在 JAR 的
/META-INF/MANIFEST.MF
文件中包含以下条目:然后它将以通常的方式作为类路径资源使用。您确实必须以某种方式指示 Maven 生成这样的 MANIFEST.MF 文件。
There are two workarounds:
Don't use the JAR as executabele JAR, but as library.
Obtain the root location of the JAR file and get the properties file from it.
None of both are recommended approaches! The recommend approach is to have the following entry in JAR's
/META-INF/MANIFEST.MF
file:Then it'll be available as classpath resource the usual way. You'll really have to instruct Maven somehow to generate the
MANIFEST.MF
file like that.编辑:这是为了回应您的评论:
您需要确保属性文件位于类路径上,并且具有启动 jar 文件的 java 调用的正确根目录。如果你的路径是
stuff/things.properties
并且运行时位置是
/opt/myapp/etc/stuff/things.properties
并且 jar 文件位于
/opt/myapp/bin/myjar
那么你需要启动为
/path/to /java -cp "/opt/myapp/etc:/opt/myapp/bin/myjar.jar" my.pkg.KavaMain
在开发环境中使用这种配置可能会令人厌烦,幸运的是,有 maven exec 插件 将为您提供正确的启动场景。
原始答案:
您想阅读maven 资源插件< /a>.
基本上你想添加这样的东西:
到你的 pom.xml 假设你的属性文件与你的 java 源文件一起 - 实际上它应该在 src/main/resources 中。
EDIT: this is to respond to your comment:
You need to make sure that the properties file is on the class path with the right root for the java invocation that stars up the jar file. if your path is
stuff/things.properties
and the runtime location is
/opt/myapp/etc/stuff/things.properties
and the jar file is in
/opt/myapp/bin/myjar
then you need to launch as
/path/to/java -cp "/opt/myapp/etc:/opt/myapp/bin/myjar.jar" my.pkg.KavaMain
working with this kind of config can be irksome in a dev environment, luckily, there's the maven exec plugin that will get you the right kind of launch scenario.
Original Answer:
You want to read about the maven resources plugin.
Basically you want to add something like this:
to your pom.xml assuming that you're propertis file is with your java sources -- really it should be in src/main/resources.
我也遇到了类似的问题,这个帖子给了我很大的帮助!仅供参考,我修改了 Ant 构建文件来制作清单,然后在 JAR 时指定该清单 我的服务器端代码:
I had a similar problem, and this thread was a big help! FYI, I modified my Ant buildfile to do the MANIFEST-making, then designated that manifest when JAR-ing my server-side code:
感谢所有这些代码为我解决了这个 url 获取我们可运行的 jar 位置
我必须轻松地读取我的属性文件,因此将您的属性文件放在可运行的 jar 文件位置
thanks to all this code work out for me this url get our runnable jar location
so easily i have to read my properties file so put your properties file in your runnable jar file location