日期:2014-05-20  浏览次数:20702 次

打包
如果写好了一个java程序如何打包成双击可直接运行的可执行文件呀!

------解决方案--------------------
使用 ant 吧,很方便的,在当前工程下新建一个 build.xml,并假设源代码放在 src 目录下的,用到的其他类库放在 lib 目录下,在当前工程下执行 ant 后,会在 mybuild/jar 下生成 myprogram-1.0.jar 的文件,并拷贝了类库,点击就可以直接运行了,若执行 ant clean 可以将 ant 生成的东西全部删除,不影响源代码。

<?xml version= "1.0 "?>
<project name= "myprogram " default= "tojar ">
<property name= "project " value= "myprogram " />
<property name= "ver " value= "1.0 " />
<property name= "work.dir " value= ". " />
<property name= "lib " value= "${work.dir}/lib " />
<property name= "src " value= "${work.dir}/src " />
<property name= "build " value= "${work.dir}/mybuild " />
<property name= "classes " value= "${build}/classes " />
<property name= "jardir " value= "${build}/jar " />
<property name= "jarlib " value= "${jardir}/lib " />
<property name= "jarfile " value= "${jardir}/${project}-${ver}.jar " />

<path id= "classpath ">
<fileset id= "classpathfileset " dir= "${lib} ">
<include name= "*.jar "/>
</fileset>
</path>
<target name= "init ">
<mkdir dir= "${classes} " />
<mkdir dir= "${jardir} " />
<mkdir dir= "${jarlib} " />
</target>

<target name= "complie " depends= "init ">
<javac srcdir= "${src} " destdir= "${classes} " source= "1.5 " encoding= "gbk ">
<include name= "**/*.java "/>
<classpath refid= "classpath "/>
</javac>
</target>

<target name= "jar " depends= "tojar ">
<jar destfile= "${jarfile} ">
<fileset dir= "${classes} "/>
<manifest>
<!-- Main-Class 的 value 放入需要执行的入口类(需要有 main 方法) -->
<attribute name= "Main-Class " value= "com.main.Main "/>
<!-- Class-Path 的 value 放入需要用到的类库,需要全部加上去用空格分开 -->
<attribute name= "Class-Path " value= "lib/xml-apis.jar lib/... "/>
</manifest>
</jar>
<copy todir= "${jarlib} ">
<fileset refid= "classpathfileset "/>
</copy>
</target>

<target name= "clean " depends= "init ">
<delete dir= "${build} "/>
</target>
</project>