
正文
java代码打包命令 java代码怎么打包成电脑软件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何把java程序打包成.jar?
打包成一般java代码打包命令的jar包java代码打包命令的步骤如下:
1.输入如下命令即可:
Cmd代码
jar
cvf
counter.jar
-C
bin
.
其中java代码打包命令,“-C
bin”实际上是告诉jar命令先cd到bin目录下java代码打包命令,再在此目录执行没有参数“-C
bin”的命令java代码打包命令,等价于:
2.Cmd代码
cd
bin
jar
cvf
counter.jar
.
//
"."代表当前路径
相关问答
Q1: 如何用jar命令对java工程进行打包
有时候为了更方便快捷的部署和执行Java程序,要把java应用程序打包成一个jar包。而这个基础的操作有时候也很麻烦,为了方便java程序员们能够方便的打包java应用程序,下面对jar命令进行介绍,并举出几个简单例子针对不同情况进行打包。
一. jar命令用法:
在cmd命令窗口下输入jar,回车,就会提示改命令的用法:
二.例子(这里介绍的都是生成双击可执行的jar包):
1.首先介绍如何在命令行下执行打包程序。
1.1.没有包结构的最简单的工程。
(1)在c:盘下新建文件“HelloWorld.java”:
1 public class HelloWorld
2 {
3 public static void main(String[] args){
4 System.out.println("Hello world!");
5 }
6 }
(2)在命令行下输入c:\javac HelloWorld.java,在c:盘下编译生成“HelloWorld.class”。
(3)在c:盘下新建文件“menefest”(没有后缀名):
Main-Class: HelloWorld
注意最后要有一个空行,否则会出现找不到类的错误。
(4)打包:输入c:\jar cvmf menifest HelloWorld.jar HelloWord.jar HelloWorld.class,在c:盘下生成“HelloWorld.jar”。
(5)执行:输入c:\java -jar HelloWord.jar。屏幕回显“HelloWorld”。
1.2.有包结构的java工程。
(1)在c:\com\gosyl\demo\下新建文件“HelloWorld.java”:
package com.gosyl.demo;
public class HelloWorld{
public static void main(String[] args){
System.out.println("HelloWorld");
}
}
(2)在命令行下输入c:\javac com/gosyl/demo/HelloWorld.java,在c:\com\gosyl\demo\文件夹下编译生成“HelloWorld.class”。
(3)在c:盘下新建文件“menefest2”(没有后缀名):
Main-Class: com.gosyl.demo.HelloWorld
注意最后要有一个空行,冒号后面要一个空格。否则会出现找不到类的错误。
(4)打包:输入c:\jar cvmf menifest2 HelloWorld.jar HelloWord.jar com/,在c:盘下生成“HelloWorld.jar”。
(5)执行:输入c:\java -jar HelloWord.jar。屏幕回显“HelloWorld”。
1.3.引用到外部jar包的java工程。
(1)在c:\com\gosyl\demo\下新建文件“Car.java”:
package com.gosyl.demo;
public class Car
{
public static void main(String[] args){
Light.on();
}
}
(2)在c:\com\gosyl\demo\下新建文件“Light.java”:
package com.gosyl.demo;
class Light
{
public static void on(){
System.out.println("Light is on!");
}
}
(3)打包1:输入c:\jar cvf Light.jar com/gosyl/demo/Light.class,在c:盘下生成“Light.jar”。
(4)在c:盘下新建文件“menefest-car”(没有后缀名):
Main-Class: com.gosyl.demo.Car
Class-Path: Light.jar
注意最后要有一个空行,冒号后面要一个空格。否则会出现找不到类的错误。
(5)打包2:输入c:\jar cvmf menifest-car Car.jar com/gosyl/demo/Car.class,在c:盘下生成“Car.jar”。
(6)执行:输入c:\java -jar Car.jar。屏幕回显“Light is on”。
三.总结
1.清单文件menifest,对格式要求很高,注意每个冒号后面要有一个空格,文件最后要有一个空行。
2.对于引用到外部jar包的工程,需要在manifest中定义好Class-Path属性。
3.对于双击.bat文件出现刷屏现象的情况,请直接把.bat文件的内容键入命令行执行。
4.对于重复签名的jar包,在META-INF里面会出现多个签名文件,删除掉多余的,保存其中一个才能保证jws正常启动。
Q2: 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比较好。
Q3: 如何将java源代码打包生成jar?
工具:
eclipse
方法:
1、启动eclipse;
2、在eclipse中建立好工程与类并写好代码;
3、点击“File-Export”;
4、在弹击的界面中选择“Java-JAR file”,再点击“Next”;
5、选择要打包的文件,再点击“Browse”;
6、在弹出的界面中选择好打包后的文件的存放路径,再输入文件名,最后点击“保存”;
7、点击“Finish”;
8、打包成功。
Q4: 如何把使用命令行或者Eclipse将Java程序打包为jar文件
如何把写好的Java程序打包为jar文件呢?
一、用Eclipse来创建
1、选择项目java代码打包命令,点击右键java代码打包命令,选择“export”;
2、选择"java-Runnable JAR file";
3、选择保存的路径,点击“Finish”即可。
二、命令行的方式java代码打包命令:
1.打包
jar cf JAR文件名称 程序文件名称或者程序所在的文件夹
举例:
jar cf MyApp.jar /home/xxx/xxx
2.查看一个jar文件的内容
jar tvf JAR文件名称
举例:
jar tvf MyApp.jar
3.将一个jar文件解压缩
jar xf JAR文件名称
举例:
jar xf MyApp.jar
4.往压缩包里面增加文件
jar xf JAR文件名称 添加的文件或者其他的jar文件
举例:
jar xf MyApp.jar Test.class
5.更新一个jar文件
jar uf JAR文件名称 更新的文件或者其他的jar文件
举例:
jar uf MyApp.jar Test.class
6.运行一个jar程序
java -jar JAR文件名称
举例:
java -jar MyApp.jar
7.创建一个可以运行的jar文件
首先必须有一个主类(Main Class);接着创建一个叫做MANIFEST.MF的文本,然后把以下内容拷贝进去。
Manifest-Version: 1.0
Main-Class: YourMainClass
这个YourMainClass就是程序中包含main方法的那个class,下面的语句用来创建可执行的jar文件。
jar cvfm MyApp.jar MANIFEST.MF 文件或文件夹路径
Q5: 如何把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,没有可忽略)
以上只是打成可执行程序的基础属性内容,若楼主也需要其它属性,可参阅:
2、到已经编译好的class目录,使用命令Jar -cvmf . 使用将程序打包xxx.jar
3、将打包好的程序及其所依赖的其他jar包一同部署到Linux下,使用命令java -jar xxx.jar启动程序
若楼主有shell脚本经验,也可将启动命令写成脚本,并加上些jvm调优参数则更好
以上三步即完成将Java工程打包成可执行程序,打成的jar包在windows、Linux下均可使用。
有问题欢迎提问,满意请采纳,谢谢!
关于java代码打包命令和java代码怎么打包成电脑软件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






