導航:首頁 > 程序命令 > java打包命令

java打包命令

發布時間:2022-01-18 21:11:01

java怎麼打包成jar

^^java教程^^《製作可執行JAR》本文闡述了如何把一個不可執行的 JAVAArchive(JAR)文件變成可執行,而不用直接操作manifest文件。你會學到寫出短小的一個程序,通過運行java-jar命令或在像windows一樣的操作系統裡面用雙擊滑鼠運行任何JAR文件。

你可以很容易地把應用程序的一整套class文件和資源文件打包到一個JAR中。事實上這就是jar文件存在的一個目的。另外一個目的就是讓用戶能很容易地執行被打包到jar文件裡面的應用程序。那麼為什麼jar文件僅僅作為文件在整個java裡面占據了次要的地位,而本地執行則被忽視?

要執行一個jar文件,你可以使用java命令的-jar選項。舉一個例子來說,假如你有個名叫myjar.jar的文件。這個jar是可以運行的,你可以運行它:java-jarmyjar.jar.另外一個辦法就是,當JavaRuntimeEnvironment(JRE)已經被安裝到一個像windows的操作系統上,將jar文件與JVM關聯(關聯 java.exe跟jar文件)在一起你就可以通過雙擊jar來運行這個應用程序。當然,jar文件必須是可執行的。

現在的問題是:如何做一個可以執行的jar?

manifest文件以及Main-class入口

在大多數jar中,都在一個叫META-INF的目錄裡面保存了一個叫MANIFEST.MF的文件。那個文件裡面,

包含了一個特殊表項名字叫Main-Class,告訴java-jar命令應該執行哪個class.
問題是你必須為manifest文件手工加入適當表項,而且必須在一定的位置和用一定的格式。不幸的是,不是每個人都喜歡打開寫字板編輯配置文件。

讓API幫你完成任務

自從java1.2發布以來,一個叫java.uil.jar包的出現,讓你能夠方便處理jar文件。(注意:該包基於java.util.zip)特別地,jar包讓你通過Mainfest類,可以容易操作那些manifest文件.

就讓我們用這個API寫一個程序吧。首先,這個程序必須知道三樣東西:

1。我們要使之可運行的jar文件。
2。運行jar的主類(這個類必須包含在jar中)。

3。輸出新jar文件的文件名,因為我們不能簡單地覆蓋原來的文件。

編寫程序

上面列表的三點要求將組成我們的程序的參數。現在,讓我們為這個程序選擇一個適當的名字。

MakeJarRunnable聽起來覺得怎樣?

為main方法檢查參數

假設我們的main方法入口點是一個標準的main(String[])方法。我們應該這樣檢查程序的參數:

if(args.length!=3){
System.out.println("Usage:MakeJarRunnable" "<jarfile><Main-Class><output>");

System.exit(0);

}

請注意參數列表是如何描述的,因為這在以下代碼中是很重要的。參數的次序和內容不是固定的;

然而,如果你要改變他們的話,要記住響應修改其他代碼。

訪問jar和jar的manifest文件

第一,我們必須創建一些了解jar和manifest的對象:

//CreatetheJarInputStreamobject,andgetitsmanifest

JarInputStreamjarIn=newJarInputStream(newFileInputStream(args[0]));

Manifestmanifest=jarIn.getManifest();

if(manifest==null){

//

manifest=newManifest();

}

設置Main-Class屬性

我們把Main-Class入口放到manifest文件的main屬性部分。一旦從manifest對象獲得這個屬性,就可以設置需要的 mainclass。然而,如果main-Class屬性已經存在原來的jar當中又如何呢?這里我們只是簡單地輸出一個警告然後退出。我們能加入一個命令行參數告訴程序使用新的值,而代替了舊的那個:

Attributesa=manifest.getMainAttributes();

StringoldMainClass=a.putValue("Main-Class",args[1]);

//Ifanoldvalueexists,telltheuserandexit

if(oldMainClass!=null){

System.out.println("Warning:oldMain-Classvalueis:"

oldMainClass);

System.exit(1);

}

輸出新的JAR

我們需要創建一個新的JAR文件,所以我們必須使用JarOutputStream類。注意:

我們必須確定我們不用跟輸入文件相同的名字作為輸出文件的名字。還有一個方案就是,程序應該考慮到一種情況,就是兩個jar文件都是相同的,促使用戶覆蓋原來的文件,如果他願意這么做的話。然而,我在保留了這一點,作為讀者的一個練習。從如下代碼開始:

System.out.println("Writingto" args[2] "...");

JarOutputStreamjarOut=newJarOutputStream(newFileOutputStream(args[2]),manifest);

我們必須從輸入JAR寫每個表項到輸出的JAR,所以迭代每個表項:

//

byte[]buf=newbyte[4096];

//Iteratetheentries

JarEntryentry;

while((entry=jarIn.getNextJarEntry())!=null){

//

if("META-INF/MANIFEST.MF".equals(entry.getName()))continue;

//WritetheentrytotheoutputJAR

jarOut.putNextEntry(entry);

intread;

while((read=jarIn.read(buf))!=-1){

jarOut.write(buf,0,read);

}

jarOut.closeEntry();

}

//Flushandcloseallthestreams

jarOut.flush();

jarOut.close();

jarIn.close();

完成程序

當然,我們必須把這些代碼放到一個類的main方法裡面,並且需要一大堆import代碼。完整程序:

http://www.javaworld.com/javaworld/javatips/javatip127/MakeJarRunnable.zip

程序使用例子

讓我們把這個程序應用到一個例子裡面來。假設你有一個應用程序,該程序的入口點是一個叫HelloRunnableWorld的類,再假設你已經創建了一個jar叫myjar.jar,包含了整個程序。運行MakeJarRunnable:

javaMakeJarRunnablemyjar.jarHelloRunnableWorldmyjar_r.jar

正如前面提到的,注意一下我的參數順序。如果你忘記了順序,沒有參數運行一下程序,它會響應出現一個用法提示信息。

嘗試對myjar.jar運行java-jar命令。然後對myjar_r.jar。注意區別不同!好了,你完成了這一切了,瀏覽一下每個jar的manifest文件(META-INF/MANIFEST.MF)

Ⅱ java如何打包

建議使用ANT打包工具,下載地址:http://apache.justdn.org/ant/binaries/apache-ant-1.6.5-bin.zip

此工具用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"/>
< todir="${build.dest}/META-INF">
<fileset dir="${top.dir}">
<include name="LICENSE"/>
</fileset>
</>
<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='http://jakarta.apache.org/oro/' target=_top><img 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>

< todir="${docs.dest}/images" filtering="no">
<fileset dir="${docs.src}/images">
<include name="**/*.gif"/>
<include name="**/*.jpeg"/>
<include name="**/*.jpg"/>
</fileset>
</>

<mkdir dir="${docs.dest}/classes"/>
<mkdir dir="${docs.dest}/classes/examples"/>
< todir="${docs.dest}/classes/examples" filtering="no">
<fileset dir="${build.dest}/examples">
<include name="MatcherDemoApplet.class"/>
</fileset>
</>
<mkdir dir="${docs.dest}/classes/org"/>
< todir="${docs.dest}/classes/org" filtering="no">
<fileset dir="${build.dest}/org">
<include name="**/*.class"/>
</fileset>
</>
</target>

<!-- package target -->

<target name="package" depends="jar,javadocs,docs"
description="Creates a distribution directory tree.">
<mkdir dir="${final.dir}"/>
< todir="${final.dir}/src">
<fileset dir="${code.src}"/>
</>
<!-- BEGIN_REMOVE_THIS -->
<!-- Remove this when there's a first draft of the manual. -->
< todir="${final.dir}/docs">
<fileset dir="${docs.dest}">
<exclude name="manual/**"/>
</fileset>
</>
<!-- END_REMOVE_THIS -->

< file="${top.dir}/build.xml" tofile="${final.dir}/build.xml"/>
< file="${top.dir}/build.properties"
tofile="${final.dir}/build.properties"/>

< file="${top.dir}/LICENSE" tofile="${final.dir}/LICENSE"/>
< file="${top.dir}/ISSUES" tofile="${final.dir}/ISSUES"/>
< file="${top.dir}/CHANGES" tofile="${final.dir}/CHANGES"/>
< file="${top.dir}/COMPILE" tofile="${final.dir}/COMPILE"/>
< file="${top.dir}/CONTRIBUTORS"
tofile="${final.dir}/CONTRIBUTORS"/>
< file="${top.dir}/README" tofile="${final.dir}/README"/>
< file="${top.dir}/STYLE" tofile="${final.dir}/STYLE"/>
< file="${top.dir}/TODO" tofile="${final.dir}/TODO"/>
< 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 將指定的文件進行打包如何實現在線等哦!

在命令行下打包jar使用如下命令:jarcvffilename.jarfoldername可以使用JAR命令進行打包下面是jar命令的幫助說明:用法:jar{ctxui}[vfm0Me][jar-file][manifest-file][entry-point][-Cdir]files選項包括:-c創建新的歸檔文件-t列出歸檔目錄-x解壓縮已歸檔的指定(或所有)文件-u更新現有的歸檔文件-v在標准輸出中生成詳細輸出-f指定歸檔文件名-m包含指定清單文件中的清單信息-e為捆綁到可執行jar文件的獨立應用程序指定應用程序入口點-0僅存儲;不使用任何ZIP壓縮-M不創建條目的清單文件-i為指定的jar文件生成索引信息-C更改為指定的目錄並包含其中的文件如果有任何目錄文件,則對其進行遞歸處理。清單文件名、歸檔文件名和入口點名的指定順序與"m"、"f"和"e"標志的指定順序相同。示例1:將兩個類文件歸檔到一個名為classes.jar的歸檔文件中:jarcvfclasses.jarFoo.classBar.class示例2:使用現有的清單文件"mymanifest"並將foo/目錄中的所有文件歸檔到"classes.jar"中:jarcvfmclasses.jarmymanifest-Cfoo/.下文假設編譯後的class文件在bin目錄下

Ⅳ 怎麼將JAVA打包

jar命令祥解:

用法:jar {ctxu}[vfm0Mi] [jar-文件] [manifest-文件] [-C 目錄] 文件名 ...

選項:

-c 創建新的存檔
-t 列出存檔內容的列表
-x 展開存檔中的命名的(或所有的〕文件
-u 更新已存在的存檔
-v 生成詳細輸出到標准輸出上
-f 指定存檔文件名
-m 包含來自標明文件的標明信息
-0 只存儲方式;未用ZIP壓縮格式
-M 不產生所有項的清單(manifest〕文件
-i 為指定的jar文件產生索引信息
-C 改變到指定的目錄,並且包含下列文件:

如果一個文件名是一個目錄,它將被遞歸處理。

清單(manifest〕文件名和存檔文件名都需要被指定,按'm' 和 'f'標志指定的相同順序。

jar cvf classes.jar Foo.class

這樣就把Foo.class打包成了classes.jar

不過還是建議樓主取下一個eclipse,然後選擇文件(file)->導出(export)->jar,然後一步步按next,最後選擇你得Main方法所在的類,就行了。。
這樣方便些。

Ⅳ java打包好的jar程序,怎麼在DOS運行

第一步:首先你必須設置classpath,設置的命令如下: SET CLASSPATH=.;f:\路徑名稱\打包的包名.jar第二步:顯示jar文件,命令是: jar -tvf 類名.jar

Ⅵ 如何打包成可在命令行利用java執行的jar文件

jar -cef test.CardLayoutDemo CardLayoutDemo.jar test
以上命令及參數的含義如下:
jar命令為java自帶的專用打包工具;
c代表生成新的jar包;
e代表可執行的類,亦即main方法所在的類。書寫時要加上包名,在本例中是後面的test.CardLayoutDemo;
f代表生成的jar包的名稱,在本例中是CardLayoutDemo.jar。此包名可以隨意命名,沒有規定;
test最後面的這個參數表示將test目錄下的所有文件都打包放到新的jar包中。

Ⅶ 怎樣在cmd中,將java打包為jar包~

在命令行下打包jar使用如下命令:
jar cvf filename.jar foldername

可以使用JAR命令進行打包
下面是jar命令的幫助說明:
用法:jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
選項包括:
-c 創建新的歸檔文件
-t 列出歸檔目錄
-x 解壓縮已歸檔的指定(或所有)文件
-u 更新現有的歸檔文件
-v 在標准輸出中生成詳細輸出
-f 指定歸檔文件名
-m 包含指定清單文件中的清單信息
-e 為捆綁到可執行 jar 文件的獨立應用程序
指定應用程序入口點
-0 僅存儲;不使用任何 ZIP 壓縮
-M 不創建條目的清單文件
-i 為指定的 jar 文件生成索引信息
-C 更改為指定的目錄並包含其中的文件
如果有任何目錄文件,則對其進行遞歸處理。
清單文件名、歸檔文件名和入口點名的指定順序
與 "m"、"f" 和 "e" 標志的指定順序相同。
示例 1:將兩個類文件歸檔到一個名為 classes.jar 的歸檔文件中:
jar cvf classes.jar Foo.class Bar.class
示例 2:使用現有的清單文件 "mymanifest" 並
將 foo/ 目錄中的所有文件歸檔到 "classes.jar" 中:
jar cvfm classes.jar mymanifest -C foo/ .

下文假設編譯後的class文件在bin目錄下

Ⅷ 如何把java打包成linux下的可執行程序

使用非工具(即使用命令)將Java工程打成可執行jar步驟如下:
1、准備MANIFEST文件(注意不要.MF後綴),MANIFEST文件內容如下:
Manifest-Version: 1.0(版本號,必須)
Created-By: xxx(創建者,可忽略)
Main-Class: com.kjt.wms.utils.ServiceStart(主程序,必須)
Class-Path: xxx/xxxx.jar(依賴的jar,沒有可忽略)
以上只是打成可執行程序的基礎屬性內容,若樓主也需要其它屬性,可參閱:
http://blog.csdn.net/zh520qx/article/details/43792693
2、到已經編譯好的class目錄,使用命令Jar -cvmf . 使用將程序打包xxx.jar

3、將打包好的程序及其所依賴的其他jar包一同部署到Linux下,使用命令java -jar xxx.jar啟動程序
若樓主有shell腳本經驗,也可將啟動命令寫成腳本,並加上些jvm調優參數則更好

以上三步即完成將Java工程打包成可執行程序,打成的jar包在windows、Linux下均可使用。

有問題歡迎提問,滿意請採納,謝謝!

Ⅸ java怎樣調用maven打包命令

你是想要引入maven的包?然後通過java代碼調用打包指令嗎?
你如果本地有配置maven的話,你可以嘗試使用java去調用cmd指令來執行maven指令。
另外你可以在eclipse里安裝M2E插件,下載一下 M2E的源代碼 通過 alt shift F1可以看到eclipse中一個view 是用哪個類的,alt shift f2 可以看一個菜單action的代碼是哪個類做的。這樣你就可以跟蹤代碼來看一下 在M2E插件中是如何執行的 maven打包了~~~。

閱讀全文

與java打包命令相關的資料

熱點內容
伺服器一直崩應該用什麼指令 瀏覽:916
cm202貼片機編程 瀏覽:724
php構造函數帶參數 瀏覽:175
解壓電波歌曲大全 瀏覽:336
為啥文件夾移到桌面成word了 瀏覽:858
命令符的安全模式是哪個鍵 瀏覽:758
編程中學 瀏覽:956
單片機求助 瀏覽:993
ug加工側面排銑毛坯怎麼編程 瀏覽:271
程序員有關的介紹 瀏覽:736
支付寶使用的什麼伺服器 瀏覽:210
安卓看本地書用什麼軟體好 瀏覽:921
經傳軟體滾動凈利潤指標源碼 瀏覽:522
螢石雲視頻已加密怎麼解除 瀏覽:574
一命令四要求五建議 瀏覽:30
qq文件夾遷移不了 瀏覽:19
液體粘滯系數測定不確定度演算法 瀏覽:332
輕棧源碼 瀏覽:426
把圖片壓縮到500k 瀏覽:35
命令你自己 瀏覽:369