使用属性配置文件运行 .jar 文件

发布于 2024-12-23 03:38:01 字数 162 浏览 4 评论 0原文

我有一个包含 properties 配置文件的 jar 文件。

当我在 Netbeans 中运行它时,一切都按预期工作。

但是当我在 cmd 中运行它时 - 找不到属性文件。

为什么?

I have a jar file with properties configuration file.

when I run it in Netbeans everything works as it should.

but when I run it in a cmd - the properties file is not found.

why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从﹋此江山别 2024-12-30 03:38:01

这取决于您加载属性文件的方式。
考虑在以下帮助下加载属性:

InputStream in = getClass().getResourceAsStream("/log4j.properties");

然后使用 Properties.load(in)

这应该可以处理属性文件物理驻留在 jar 中的情况
祝你好运!

It depends on how you're loading your properties file.
Consider to load your properties with the help of:

InputStream in = getClass().getResourceAsStream("/log4j.properties");

and then use Properties.load(in)

This should handle the situation when the properties file physically resides in the jar
Good Luck!

桜花祭 2024-12-30 03:38:01

当您将属性打包到 jar 文件中时,您必须使用类加载器来定位该文件,因为它不再作为文件可见

如果属性文件位于 jar 文件的 root 位置,那么上面给出的答案就是您将使用的:

 Properties p = new Properties();
 InputStream is = MyClass.class.getResourceAsStream("/config.properities");
 if( is != null )
 {
    p.load(is);
 }

它应该返回一个 InputStream,您可以将其传递给 Properities 类进行加载。如果该调用返回 NULL,那么您需要查看属性文件相对于 jar 文件根目录的位置。

When you package the properties inside the jar file, you have to use the class loader to locate the file since it is no longer visible as a file.

If the properties file is insider the jar file at the root of the jar file then the answer given above is what you would use:

 Properties p = new Properties();
 InputStream is = MyClass.class.getResourceAsStream("/config.properities");
 if( is != null )
 {
    p.load(is);
 }

that should return an InputStream that you can pass to the Properities class to load. If that call returns a NULL then you need to see where the property file is relative to the root of the jar file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文