本文目錄一覽:
java如何打包
建議使用ANT打包工具,下載地址:
此工具用java編寫,跨平台,能實現很多功能:編譯、打包、運行、文件操作、資料庫操作、自定義任務等。
主要使用方法:在工程目錄下編寫build.xml配置文件,然後運行ant即可:
#ant
或
#java -jar ant.jar
下面給你提供一個例子,是jakarta-oro-2.0.8的包的build.xml
?xml version=”1.0″?
project default=”jar”
!– Allow properties following these statements to be overridden —
!– Note that all of these don’t have to exist. They’ve just been defined
incase they are used. —
property file=”build.properties”/
!–
property file=”.ant.properties”/
property file=”${user.home}/.ant.properties”/
property file=”default.properties”/
—
!– prepare target. Creates build directories. —
target name=”splash”
splash imageurl=”./ant_logo_medium.gif”/
/target
target name=”prepare” depends=”splash” description=”Creates build directories.”
tstamp
format property=”DATE” pattern=”yyyy-MM-dd hh:mm:ss” /
/tstamp
mkdir dir=”${build.dest}”/
mkdir dir=”${build.dest}/META-INF”/
copy todir=”${build.dest}/META-INF”
fileset dir=”${top.dir}”
include name=”LICENSE”/
/fileset
/copy
mkdir dir=”${build.tests}”/
available file=”${jakarta-site2.dir}/lib” type=”dir”
property=”AnakiaTask.present”/
/target
target name=”prepare-error” depends=”prepare”
description=”Prints error message for prepare target.”
unless=”AnakiaTask.present”
echo
AnakiaTask is not present! Please check to make sure that
velocity.jar is in your classpath.
/echo
/target
!– lib target. Compiles the library classes only —
target name=”lib” depends=”prepare”
description=”Compiles the library classes only.”
javac srcdir=”${build.src}”
destdir=”${build.dest}”
excludes=”examples/**,tools/**”
debug=”${debug}”
deprecation=”${deprecation}”
optimize=”${optimize}”/
/target
!– examples target. Compiles the example classes. —
target name=”examples” depends=”prepare,lib”
description=”Compiles the example classes.”
javac srcdir=”${build.src}”
destdir=”${build.dest}”
includes=”examples/**”
debug=”${debug}”
deprecation=”${deprecation}”
optimize=”${optimize}”/
/target
!– tools target. Compiles the tool classes. —
target name=”tools” depends=”prepare,lib”
description=”Compiles the tool classes.”
javac srcdir=”${build.src}”
destdir=”${build.dest}”
includes=”tools/**”
debug=”${debug}”
deprecation=”${deprecation}”
optimize=”${optimize}”/
/target
!– tests target. Compiles and runs the unit tests. —
target name=”tests” depends=”prepare,lib”
description=”Compiles and runs the unit tests.”
javac srcdir=”${build.src}/tests”
destdir=”${build.tests}”
debug=”${debug}”
deprecation=”${deprecation}”
optimize=”${optimize}”/
/target
!– jar target. Compiles the source directory and creates a .jar file —
target name=”jar” depends=”lib” description=”Compiles the source directory and creates a .jar file.”
jar jarfile=”${top.dir}/${final.name}.jar”
basedir=”${build.dest}”
includes=”org/**,META-INF/**”
excludes=”**/package.html,**/overview.html”
manifest
section name=”org/apache/oro”
attribute name=”Specification-Title”
value=”Jakarta ORO” /
attribute name=”Specification-Version”
value=”${version}” /
attribute name=”Specification-Vendor”
value=”Apache Software Foundation” /
attribute name=”Implementation-Title”
value=”org.apache.oro” /
attribute name=”Implementation-Version”
value=”${version} ${DATE}” /
attribute name=”Implementation-Vendor”
value=”Apache Software Foundation” /
/section
/manifest
/jar
/target
!– javadocs target. Creates the API documentation —
target name=”javadocs” depends=”prepare”
description=”Creates the API documentation.”
mkdir dir=”${javadoc.destdir}”/
javadoc packagenames=”org.apache.oro.io,org.apache.oro.text,org.apache.oro.text.regex,org.apache.oro.text.awk,org.apache.oro.text.perl,org.apache.oro.util”
sourcepath=”${build.src}”
destdir=”${javadoc.destdir}”
overview=”${build.src}/org/apache/oro/overview.html”
author=”true”
version=”true”
windowtitle=”${name} ${version} API”
doctitle=”${name} ${version} API”
header=”a href=” target=_topimg src='{@docroot}/../images/logoSmall.gif’ alt=’Jakarta ORO’ width=48 height=47 align=center border=0 hspace=1 vspace=1/a”
bottom=”Copyright © ${year} Apache Software Foundation. All Rights Reserved.”
/javadoc
replace file=”${javadoc.destdir}/overview-frame.html”
token=”{@docroot}” value=”.”/
replace dir=”${javadoc.destdir}” includes=”**/*.html”
token=”@version@” value=”${version}”/
/target
!– docs target. Creates project web pages and documentation. —
target name=”docs” depends=”prepare-error,lib,examples”
description=”Creates the project web pages and documentation.”
if=”AnakiaTask.present”
taskdef name=”anakia” classname=”org.apache.velocity.anakia.AnakiaTask”
classpath
fileset dir=”${jakarta-site2.dir}/lib”
include name=”*.jar”/
/fileset
/classpath
/taskdef
anakia basedir=”${docs.src}” destdir=”${docs.dest}/”
extension=”.html” style=”./site.vsl”
projectFile=”stylesheets/project.xml”
excludes=”**/stylesheets/** manual/** empty.xml”
includes=”**/*.xml”
lastModifiedCheck=”true”
templatePath=”${jakarta-site2.dir}/xdocs/stylesheets”
/anakia
copy todir=”${docs.dest}/images” filtering=”no”
fileset dir=”${docs.src}/images”
include name=”**/*.gif”/
include name=”**/*.jpeg”/
include name=”**/*.jpg”/
/fileset
/copy
mkdir dir=”${docs.dest}/classes”/
mkdir dir=”${docs.dest}/classes/examples”/
copy todir=”${docs.dest}/classes/examples” filtering=”no”
fileset dir=”${build.dest}/examples”
include name=”MatcherDemoApplet.class”/
/fileset
/copy
mkdir dir=”${docs.dest}/classes/org”/
copy todir=”${docs.dest}/classes/org” filtering=”no”
fileset dir=”${build.dest}/org”
include name=”**/*.class”/
/fileset
/copy
/target
!– package target —
target name=”package” depends=”jar,javadocs,docs”
description=”Creates a distribution directory tree.”
mkdir dir=”${final.dir}”/
copy todir=”${final.dir}/src”
fileset dir=”${code.src}”/
/copy
!– BEGIN_REMOVE_THIS —
!– Remove this when there’s a first draft of the manual. —
copy todir=”${final.dir}/docs”
fileset dir=”${docs.dest}”
exclude name=”manual/**”/
/fileset
/copy
!– END_REMOVE_THIS —
copy file=”${top.dir}/build.xml” tofile=”${final.dir}/build.xml”/
copy file=”${top.dir}/build.properties”
tofile=”${final.dir}/build.properties”/
copy file=”${top.dir}/LICENSE” tofile=”${final.dir}/LICENSE”/
copy file=”${top.dir}/ISSUES” tofile=”${final.dir}/ISSUES”/
copy file=”${top.dir}/CHANGES” tofile=”${final.dir}/CHANGES”/
copy file=”${top.dir}/COMPILE” tofile=”${final.dir}/COMPILE”/
copy file=”${top.dir}/CONTRIBUTORS”
tofile=”${final.dir}/CONTRIBUTORS”/
copy file=”${top.dir}/README” tofile=”${final.dir}/README”/
copy file=”${top.dir}/STYLE” tofile=”${final.dir}/STYLE”/
copy file=”${top.dir}/TODO” tofile=”${final.dir}/TODO”/
copy file=”${top.dir}/${final.name}.jar” tofile=”${final.dir}/${final.name}.jar”/
/target
!– package-zip target. Packages the distribution with ZIP —
target name=”package-zip” depends=”package”
description=”Packages the distribution as a zip file.”
zip zipfile=”${top.dir}/${final.name}.zip” basedir=”${top.dir}/”
includes=”**/${final.name}/**” excludes=”**/.cvsignore”/
/target
!– Packages the distribution with TAR-GZIP —
target name=”package-tgz” depends=”package”
description=”Packages the distribution as a gzipped tar file.”
tar tarfile=”${top.dir}/${final.name}.tar”
basedir=”${top.dir}” excludes=”**/**”
tarfileset dir=”${final.dir}/..”
include name=”${final.name}/**”/
exclude name=”**/.cvsignore”/
/tarfileset
/tar
gzip zipfile=”${top.dir}/${project}-${version}.tar.gz” src=”${top.dir}/${project}-${version}.tar”/
/target
!– Packages the distribution with ZIP and TAG-GZIP —
target name=”package-all” depends=”package-zip, package-tgz”
/target
!– Makes an attempt to clean up a little. —
target name=”clean”
description=”Removes generated artifacts from source tree.”
delete dir=”${build.dest}”/
delete dir=”${javadoc.destdir}”/
delete dir=”${final.dir}”/
delete file=”${top.dir}/${final.name}.jar”/
delete file=”${top.dir}/${final.name}.tar”/
delete file=”${top.dir}/${final.name}.tar.gz”/
delete file=”${top.dir}/${final.name}.zip”/
delete
fileset dir=”${top.dir}” includes=”velocity.log*”/
/delete
/target
/project
build.xml文件的編寫可參見ant發行版中的使用文檔
你也可以自己編寫腳本打包,使用jdk發行版中的jar命令,例如:
#jar cmf myManifestFile myFile.jar *.class
注意還需要自己編寫MANIFEST.MF文件配置包屬性。具體可以參見javadoc
當然還可以使用集成開發環境提供的打包工具,如JBuilder提供打包工具,但這樣程序的移植性不強,建議不要使用,就使用ant比較好。
如何把java程序打包成exe
將java源碼打包成exe的步驟如下:1、最簡單直接的方法,選擇想打包的程序,右鍵export;2、選擇Java選項,再選擇RunnableJARfile選項,點擊next按鈕;3、選擇程序的主類,還有Main函數的類和輸入要導出的位置,點擊finish;4、在電腦上,設置好Jar文件的打開方式(別默認用解壓縮的工具打開就行),雙擊即可運行。
Java項目寫完後怎麼怎麼打包?
Java項目的打包成jar文件:
eclipse 中選擇要發布的工程右鍵單擊,選擇export
然後在列表中選擇jar(不一定馬上看到,但是可以找到的)
確定後下一步,選擇你要發布的類,反正你要用的到的類全部選擇進去包括圖片或者音樂(音樂和圖片的引用方式有關係,另當別論),然後取個名稱,選擇路徑
再下一步兩次,然後選擇main-class,就是你運行程序時運行的哪個類,選擇後單擊finish,jar文件應該打包成功了,雙擊可以直接運行。
如何將java代碼打包成jar包
方法:
1、啟動eclipse;
2、在eclipse中建立好工程與類並寫好代碼;
3、點擊「File-Export」;
4、在彈擊的界面中選擇「Java-JAR file」,再點擊「Next」;
5、選擇要打包的文件,再點擊「Browse」;
6、在彈出的界面中選擇好打包後的文件的存放路徑,再輸入文件名,最後點擊「保存」;
7、點擊「Finish」;
8、打包成功。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/238867.html
微信掃一掃
支付寶掃一掃