Creating an Executable jar File

本文详细介绍了如何使用Java JDK的jar.exe工具将多个类文件打包成jar文件,并通过创建manifest文件指定主类来使jar文件成为可运行程序。包括创建manifest文件的方法、使用命令行创建jar文件的步骤以及如何测试jar文件。
部署运行你感兴趣的模型镜像

In Java, it is common to combine several classes in one .jar ("java archive") file.  Library classes are stored that way.  Larger projects (such as the Case Study in the AP program) use jar files.  You can create your own jar file combining several classes, too. 

jar files are created using the jar.exe utility program from JDK.  You can make your jar file runnable by telling jar.exe which class has main.  To do that, you first need to create a manifest file.  A manifest is a one-line text file with a "Main-Class" directive.  

For example:Main-Class: DanceStudio

This line must end with a newline. 

A jar file created with a main class manifest can be used both as a library and a runnable jar.  If you use it as a library, you can edit and compile any of the classes included in the jar, and add it to your project.  Then it will override the one in the jar file.

You can create a manifest file in any text editor, or even by using the MS-DOS echo command.  You can give your manifest file any name, but it’s better to use something standard, such as manifest.txt. 

Once you have a manifest and all your classes have been compiled, you need to run JDK’s jar.exe utility.  It is located in the JDK’s bin folder, the same place where javac.exe and java.exe. are.  jar.exe takes command-line arguments; if you run it without any arguments, it will display the usage information and examples.  

You need C\mywork> jar cvfm MyJarName.jar manifest.txt *.class

cvfm means "create a jar; show verbose output; specify the output jar file name; specify the manifest file name."  This is followed by the name you wish to give to your jar file, the name of your manifest file, and the list of .class files that you want included in the jar.  *.class means all class files in the current directory.

Creating a jar File in Command Prompt
Start Command Prompt.
Navigate to the folder that holds your class files:
C:\>cd \mywork
Set path to include JDK’s bin.  For example:
C:\mywork> path c:\Program Files\Java\jdk1.5.0_09\bin;%path%
Compile your class(es):
C:\mywork> javac *.java
Create a manifest file:
C:\mywork> echo Main-Class: DanceStudio >manifest.txt
Create a jar file:
C:\mywork> jar cvfm DanceStudio.jar manifest.txt *.class
Test your jar:
C:\mywork> DanceStudio.jar

您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值