使用 Java Eclipse 创建带有 .properties 文件的 Runnable JAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1507118/
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-08-12 13:36:05  来源:igfitidea点击:

Create Runnable JAR with .properties files using Java Eclipse

javaeclipsejarexport

提问by Beier

I'm trying to build a runnable JAR using Eclipse's Export function. everything works fine except several .properties files I have under the root directory.

我正在尝试使用 Eclipse 的导出功能构建一个可运行的 JAR。除了我在根目录下的几个 .properties 文件外,一切正常。

I added all the .properties files into Build Path, and they appears under 'Order and Export' tab in Java Build Path dialog.

我将所有 .properties 文件添加到 Build Path 中,它们出现在 Java Build Path 对话框的“Order and Export”选项卡下。

However, when I try to run the Export, I got the following errors:

但是,当我尝试运行导出时,出现以下错误:

Could not read JAR file 'log4j.properties'. Reason: error in opening zip file error in opening zip file error in opening zip file

无法读取 JAR 文件“log4j.properties”。原因:打开 zip 文件时出错 打开 zip 文件时出错 打开 zip 文件时出错

The Runnable JAR file is actually created, but with no .properties files in it. why does it try to export .properties files as ZIP files? how to make this work?

Runnable JAR 文件实际上已创建,但其中没有 .properties 文件。为什么它会尝试将 .properties 文件导出为 ZIP 文件?如何使这项工作?

Eclipse Build id: 20090621-0832

Eclipse 构建 ID:20090621-0832

采纳答案by lief79

This is based on memory, but I'm fairly sure that properties files aren't automatically loaded from a jar. The idea is that the properties files should be easily modifiable. Otherwise, the values might as well just be in a class file.

这是基于内存的,但我很确定属性文件不会从 jar 自动加载。这个想法是属性文件应该很容易修改。否则,这些值也可能只是在一个类文件中。

If you tried to access a properties file from anywhere else, this shouldn't be a problem.

如果您尝试从其他任何地方访问属性文件,这应该不是问题。

This is also why you seldom see jar files packaged alone. There is usually something else that needs to be loaded.

这也是你很少看到单独打包的jar文件的原因。通常还有其他东西需要加载。

Is there a reason this behavior is necessary?

这种行为是否有必要?

回答by Andre Pastore

jar files are pack and/or compressed as ZIP files, only file extension changes.

jar 文件被打包和/或压缩为 ZIP 文件,只有文件扩展名更改。

When all properties are shown in Export wizard, they are selected? When you open the jar file, there some file? *.class, META-INF, ... ?

当导出向导中显示所有属性时,它们是否被选中?当你打开jar文件时,有一些文件?*.class, META-INF, ... ?

[]'s,

[] 的,

And Past

和过去

回答by Jason S

Here's what I do and it works reliably:

这是我所做的并且它可靠地工作:

  1. put your .properties file under the source tree in your Eclipse project
  2. make sure all the .properties files in the source tree get copied to the build tree. I use ant with a <copy>task declared before the <jar>task in the same target.
  3. my classes access the properties files with getResourceAsStream()
  1. 将 .properties 文件放在 Eclipse 项目的源代码树下
  2. 确保源树中的所有 .properties 文件都被复制到构建树中。我将 ant 与在同一目标中的<copy>任务之前声明的<jar>任务一起使用。
  3. 我的类使用 getResourceAsStream() 访问属性文件

I am not sure what the Eclipse Export utility does but you might try looking at ant, as it may give you more control over the build process.

我不确定 Eclipse Export 实用程序的作用,但您可以尝试查看 ant,因为它可以让您更好地控制构建过程。

回答by Philipp Wendler

Instead of using the Export wizard to create a "Runnable JAR", you can use the wizard to create a (plain) "JAR" file. In this wizard, you can select which specific files should be added to the JAR file.

您可以使用该向导创建(普通的)“JAR”文件,而不是使用导出向导来创建“可运行 JAR”文件。在此向导中,您可以选择应将哪些特定文件添加到 JAR 文件中。

If you specify a main class in the wizard, the resulting JAR file will also be runnable (by double-clicking or with java -jar myapp.jar).

如果您在向导中指定主类,则生成的 JAR 文件也将是可运行的(通过双击或使用java -jar myapp.jar)。