简体   繁体   English

如何将属性文件附加到jar?

[英]How do I attach properties files to a jar?

I have a project that uses the serial port, and it requires two files to run, the win32.dll file (which is in the java runtime environment bin folder) and the javax.comm.properties file (which is in the java runtime environment lib folder). 我有一个使用串口的项目,它需要运行两个文件,win32.dll文件(位于java运行时环境bin文件夹中)和javax.comm.properties文件(位于java运行时环境中) lib文件夹)。 When I run the project from eclipse it works, but when I try to build a jar file for distribution, it won't work. 当我从eclipse运行项目时它可以工作,但是当我尝试构建一个jar文件进行分发时,它将无法工作。 I suspect this is because the dll and properties files aren't included in the jar. 我怀疑这是因为jar中没有包含dll和属性文件。 How do I specify that they need to be there? 我如何指定他们需要在那里?

You generally don't put dll and properties files inside the jar. 您通常不会将dll和属性文件放在jar中。 Properties files as well other jar files need to be added to the classpath. 需要将属性文件以及其他jar文件添加到类路径中。 The jar file has a manifest file that defines the classpath used. jar文件有一个清单文件,用于定义使用的类路径。 You can't edit this with eclipse. 你不能用eclipse编辑它。 You need to define an ant build.xml file and do something like this: 您需要定义一个ant build.xml文件并执行以下操作:

<jar jarfile="${dist}/MyJar.jar" basedir="${build}">
  <manifest>
    <attribute name="Main-Class" value="MyClass"/>
    <attribute name="Class-Path" value="."/>
  </manifest>
</jar>

Then put the properties file in the same folder as the jar. 然后将属性文件放在与jar相同的文件夹中。 You can run the ant target by right clicking the build.xml and selecting the "Run as Ant target". 您可以通过右键单击build.xml并选择“Run as Ant target”来运行ant目标。

If I remember correctly, placing the dll file in the bin directory of the jre will work. 如果我没记错的话,将dll文件放在jre的bin目录下就行了。

I think javax.comm.properties just need to be on your classpath. 我认为javax.comm.properties只需要在你的类路径上。 You may can add it to the top level of a jar you delivery. 您可以将它添加到您交付的罐子的顶层。

InputStream is = MainClass.class.getResourceAsStream("javax.comm.properties"); InputStream = MainClass.class.getResourceAsStream(“javax.comm.properties”); if (is == null) {properties missing....} if(is == null){property missing ....}

I think win32.dll just need to be on the %PATH%(windows) or $LD_LIBRARY_PATH(unix)...... 我认为win32.dll只需要在%PATH%(windows)或$ LD_LIBRARY_PATH(unix)上......

A jar file is just a normal zip file. jar文件只是一个普通的zip文件。 If you want to add files to it, just use a tool such as winzip. 如果要向其添加文件,只需使用winzip等工具即可。

With Ant, you can pack everything in your Jar you want to. 使用Ant,您可以将所有内容装入您想要的Jar中。 So let Ant create your Jar, not Eclipse :) 所以让Ant创建你的Jar,而不是Eclipse :)

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

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