由于官方有案例,所以很多网上的文章都说了怎么把netbeans的输出打包成一个jar, 有些叫fat-jar。
主要的用法就是修改 build.xml
但是也有以讹传讹的。首先是manifest.mf,如果是netbeans自建的工程,里面只有两行:
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
很多人提到要在这里设置main-class的值,实际上是不需要的(自建的工程)。
第二,关于打包:官方提到了修改build.xml,在</project>之前增加内容:
蓝色的内容是需要根据自己的情况修改的。
<target name="package-for-store" depends="jar">
<property name="store.jar.name" value="LmCreater"/>
<property name="store.dir" value="store"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<property name="binWork.dir" value="binWork"></property>
<property name="batfile" value="lmCreater.bat"></property>
<property name="store.binWork" value="${store.dir}/binWork"></property>
<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
<delete dir="${store.dir}"/>
<mkdir dir="${store.dir}"/>
<delete dir="${store.binWork}"/>
<mkdir dir="${store.binWork}"/>
<copydir dest="${store.binWork}" src="${binWork.dir}"/>
<copy file="${batfile}" todir="${store.dir}" />
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="${javafx.main.class}"/>
</manifest>
</jar>
<zip destfile="${store.jar}">
<zipfileset src="${store.dir}/temp_final.jar"
excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${store.dir}/temp_final.jar"/>
</target>
关键是那个红色的部分 ,Main-Class的值,官方的文档,以及网上一大抄的文档写的是 ${main.class},如果是javafx的工程,这里是不同的,要用 javafx.main.class。 这是关键。
后面就简单了,在build.xml上右键,找到package-for-store,即可生成store目录下的单文件jar以及自己需要使用的其他文件。