简体   繁体   English

使用Java Eclipse使用.properties文件创建可运行的JAR

[英]Create Runnable JAR with .properties files using Java Eclipse

I'm trying to build a runnable JAR using Eclipse's Export function. 我正在尝试使用Eclipse的Export函数构建可运行的JAR。 everything works fine except several .properties files I have under the root directory. 除了我在根目录下有几个.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'. 无法读取JAR文件'log4j.properties'。 Reason: error in opening zip file error in opening zip file error in opening zip file 原因:打开zip文件时出错打开zip文件时出错打开zip文件时出错

The Runnable JAR file is actually created, but with no .properties files in it. 实际上创建了Runnable JAR文件,但是其中没有.properties文件。 why does it try to export .properties files as ZIP files? 为什么尝试将.properties文件导出为ZIP文件? how to make this work? 如何使这项工作?

Eclipse Build id: 20090621-0832 Eclipse建立编号:20090621-0832

This is based on memory, but I'm fairly sure that properties files aren't automatically loaded from a jar. 这是基于内存的,但是我很确定不会从jar自动加载属性文件。 The idea is that the properties files should be easily modifiable. 想法是,属性文件应易于修改。 Otherwise, the values might as well just be in a class file. 否则,这些值也可能只在类文件中。

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. 这也是为什么您很少看到jar文件单独打包的原因。 There is usually something else that needs to be loaded. 通常还需要加载其他内容。

Is there a reason this behavior is necessary? 此行为是否有必要是有原因的?

Here's what I do and it works reliably: 这是我的工作,它运行可靠:

  1. put your .properties file under the source tree in your Eclipse project 将.properties文件放在Eclipse项目的源代码树下
  2. make sure all the .properties files in the source tree get copied to the build tree. 确保将源树中的所有.properties文件复制到构建树中。 I use ant with a <copy> task declared before the <jar> task in the same target. 我在同一个目标中的<jar>任务之前声明的<copy>任务中使用了ant。
  3. my classes access the properties files with getResourceAsStream() 我的班级使用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,因为它可以使您更好地控制构建过程。

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? 当您打开jar文件时,有一些文件吗? *.class, META-INF, ... ? * .class,META-INF,...?

[]'s, []的,

And Past 和过去

Instead of using the Export wizard to create a "Runnable JAR", you can use the wizard to create a (plain) "JAR" file. 您可以使用向导创建(普通)“ JAR”文件,而不是使用“导出”向导来创建“可运行的JAR”。 In this wizard, you can select which specific files should be added to the JAR file. 在此向导中,您可以选择应将哪些特定文件添加到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 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM