简体   繁体   中英

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). When I run the project from eclipse it works, but when I try to build a jar file for distribution, it won't work. I suspect this is because the dll and properties files aren't included in the jar. How do I specify that they need to be there?

You generally don't put dll and properties files inside the jar. Properties files as well other jar files need to be added to the classpath. The jar file has a manifest file that defines the classpath used. You can't edit this with eclipse. You need to define an ant build.xml file and do something like this:

<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. You can run the ant target by right clicking the build.xml and selecting the "Run as Ant target".

If I remember correctly, placing the dll file in the bin directory of the jre will work.

I think javax.comm.properties just need to be on your classpath. You may can add it to the top level of a jar you delivery.

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

I think win32.dll just need to be on the %PATH%(windows) or $LD_LIBRARY_PATH(unix)......

A jar file is just a normal zip file. If you want to add files to it, just use a tool such as winzip.

With Ant, you can pack everything in your Jar you want to. So let Ant create your Jar, not Eclipse :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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