打包 Java 应用程序时,jar 文件中不包含属性文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2070065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 19:14:46  来源:igfitidea点击:

Properties file isn't included in jar file when package Java application

javanetbeans

提问by Chan Pye

I have developed Java desktop application using Netbeans. In my application, I used some properties files and I placed it under the Project folder so that I can access it by using code like that

我已经使用 Netbeans 开发了 Java 桌面应用程序。在我的应用程序中,我使用了一些属性文件并将其放在 Project 文件夹下,以便我可以使用类似的代码访问它

 Properties props = new Properties();
 props.load(new FileInputStream("config.properties"));

But when I deploy and package my application to .jar file, I can't find out where properties files are and my application can not read value from properties files.

但是当我将我的应用程序部署和打包到 .jar 文件时,我无法找到属性文件的位置,并且我的应用程序无法从属性文件中读取值。

How can I fix that bug, and where should I place my properties file and load it?

我该如何修复那个错误,我应该在哪里放置我的属性文件并加载它?

回答by Adeel Ansari

Put it under /src/resources/, then use it like below.

把它放在 下/src/resources/,然后像下面一样使用它。

ResourceBundle props = ResourceBundle.getBundle("resources.config");

NetBeans doesn't include the files under your project directory. Further, you need to build the project, in order to let NetBeans put the propertiesfile inside your /classesdirectory. Then you should be able to pick it up, by running your application or just a particular related Java class.

NetBeans 不包括项目目录下的文件。此外,您需要构建项目,以便让 NetBeans 将properties文件放入您的/classes目录中。然后您应该能够通过运行您的应用程序或只是一个特定的相关 Java 类来获取它。

Cheers.

干杯。

回答by PMorganCA

I started with ralphie boy's solution and it didn't work. Well, it would have worked if I had done what he said. Here's what I did and maybe it will add some useful detail.

我从拉菲男孩的解决方案开始,但没有奏效。好吧,如果我按照他说的去做,它就会奏效。这就是我所做的,也许它会添加一些有用的细节。

I have always named my packages after the project, so I was not aware that in NetBeans there is literally, a package named <default package>. When I dragged my config.properties file up to the Source Packages level, low and behold a <default package>was made. It got placed in the src folder and built at the root in the dist .jar file. After that, ralphie boy's solution was a cut and paste.

我总是以项目命名我的包,所以我不知道在 NetBeans 中确实有一个名为<default package>. 当我将我的 config.properties 文件拖到 Source Packages 级别时,会发现一个<default package>。它被放置在 src 文件夹中,并在 dist .jar 文件的根目录下构建。在那之后,拉菲男孩的解决方案是剪切和粘贴。

If you want your resource bundle or config file to be inthe package, you have to include the path to it in the baseName string, like

如果你希望你的资源包或配置文件包中,你必须在 baseName 字符串中包含它的路径,比如

ResourceBundle f = ResourceBundle.getBundle("pkgname/config");

or

或者

ResourceBundle f = ResourceBundle.getBundle("com/mycompany/pkgname/config");

回答by wpgreenway

To include a properties file that exists in the classpath (i.e. if it is somewhere under your /src directory when you build the jar file), you can use getResourceAsStream() like this:

要包含存在于类路径中的属性文件(即,如果在构建 jar 文件时它位于 /src 目录下的某个位置),您可以像这样使用 getResourceAsStream():

Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("/config.properties"));

Note the leading slash on the filename. You can also use this to get to files within different packages in your project.

请注意文件名上的前导斜杠。您还可以使用它来获取项目中不同包中的文件。

properties.load(this.getClass().getResourceAsStream("/com/company/project/config.properties"));

回答by ralphie boy

In your Java project, in the default package (i.e. .../src/), put this file: messages.properties

在您的 Java 项目中,在默认包(即 .../src/)中,放置此文件:messages.properties

In your code, put this:

在你的代码中,把这个:

ResourceBundle f = ResourceBundle.getBundle("messages");

This approach has the charming aspect that it actually works!

这种方法有一个迷人的方面,它实际上有效!

(Someone edited and civilized my original post, which had a lot of SCREAM in it. I was seeing some pretty invalid answers getting votes, which I haven't seen for a long time on this site.)

(有人编辑并文明化了我原来的帖子,里面有很多尖叫声。我看到一些非常无效的答案得到了投票,我很久没有在这个网站上看到了。)