1. 正常打包
在項目上右鍵,然後選擇【Build】即可.
打包完成後,會在項目的主目錄下,生成一個[dist]檔案夾。可以看到所有的第三方包都被放到了lib目錄下,而不是形成單個的jar包
2. 打包第三方jar
在NetBeans界面的左上方,切換到【Files】标簽。打開項目目錄下的build.xml檔案。
在build.xml的最後一行</project>的前面,加入以下代碼。其中,value=”ClientMain”可以改成其他名字。比如你想生成abc.jar,那麼改成value=”abc”就可以了。注意!!! Netbeans 6.9.1不允許改生成的名字,是以最後生成的temp_final.jar即為所求,生成後自行改名即可!而生成的MarsRoverViewer.jar!!!!
<target name="package-for-store" depends="jar">
<!-- Change the value of this property to be the name of your JAR, minus the .jar extension. It should not have spaces. -->
<property name="store.jar.name" value="ClientMain"/>
<property name="store.dir" value="store"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
<delete dir="${store.dir}"/>
<mkdir dir="${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="${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>
改完build.xml後儲存,在該檔案上右鍵,依次選擇【Run Target】【Other Targets】【package-for-store】
等待一會兒運作完之後,可以在項目主目錄下發現一個[store]檔案夾,裡面就是打包好的一個單一的jar包.