2009年2月9日 星期一

methods for creating a self-contained .jar with data file


Given Main.java, data1.dat, data2.dat,
methods for creating a self-contained .jar with data file
0.use getResourceAsStream to get jar data at package root
InputStream is = Main.class.getResourceAsStream("/"+dataName);

1.add to all *.java source
package my.package;

2.compile with package start location
java -d . *.java

3.vi manifest.txt
#Class-Path: my.package
Main-Class: my.package.Main

4.produce jar file with data files
jar cvfm mypackage.jar manifest.txt my data1.dat data2.dat

5.run by
java -jar mypackage.jar
java -cp mypackage.jar my.package.Main