用JDK工具纯手工打造Jar文件的方式

   最近心血来潮,很想研究用jdk自带的小米步枪打包工具来打造自己的Jar程序。于是到网络上去爬爬看。唉,怎一个失望了得。翻阅所有我找到的信息,基本上找到的东西都不是很详细,有很多的朋友都是顺手牵来的文章,没有一点新意。于是只好自己来摸着瞎子过河,一步一步的试了,终于弄出一点眉目了。希望朋友们分享一下,若有不对,请指出来先!下面开始了:

首先明确一下相关文件目录结构。

 

   源文件目录结构:

 

 

   编译产生的文件目录:

 Hello.java源文件代码:

 

World.java源文件

 

好,顺便说一下jdk5.0中的Jar命令格式:

 

        jar  {ctux}[vfm0Mi][jar-file][manifest-file][-C dir] file

 

其中{}中的参数是必选的Jar命令的子命令,和Jar一起协同工作来完成打包任务。关于命令的详解我就不再罗嗦了,网上的信息比较多。

 

打包class文件是me包中的Hello.class 和you包中的World.class文件,二者存在与C:/classes目录中。(上图)

另外jar命令打包的时候有两种方式,一是绝对路径打包方式,二是相对路径打包方式。打包生成的jar文件位于当前目录下。

 

a)绝对路径打包方式:

其中:

 -c参数指定要打包的所有文件/目录的根目录位置(顶级包对应目录夹的上一目录)。

 -C 参数后的参数是File 参数,是要打包的文件名称,在这里/.表示当前目录(classes目录)下的所有文件、目录都将被打包。

上面的打包方式可以也科技用下面的方法:

即先打包,后更新包。

b)相对路径打包方式

还有如果程序要运行,必须要填写下面的信息,这是位于META-INF/MANIFEST.MF中的文本信息:

 

      

     注意:Main-class是指定主类的参数,Main-class 参数后要留一行空行。

 

 附:Jar命令的英文解释(个人感觉比JDK的好一些,详细一些

C

Creates a new archive to file named jarfile (if f is specified) or to standard output (if f and jarfile are omitted). Add to it the files and directories specified by inputfiles. 

t

Lists the table of contents from jarfile (if f is specified) or standard input (if f and jarfile are omitted). If inputfiles is specified, only those specified files and directories are listed. Otherwise, all files and directories are listed.

 

u

Updates an existing file jarfile (when f is specified) by adding to it files and directories specified by inputfiles.

x

Extracts files and directories from jarfile (if f is specified) or standard input (if f and jarfile are omitted). If inputfiles is specified, only those specified files and directories are extracted. Otherwise, all files and directories are extracted. 

i

Generate index information for the specified jarfile and its dependent jar files.

 

f

Specifies the file jarfile to be created (c), updated (u), extracted (x), indexed (i), or viewed (t). The f option and filename jarfile are a pair -- if present, they must both appear. Omitting f and jarfile accepts a "jar file" from standard input (for x and t) or sends the "jar file" to standard output (for c and u). 
 

v

Generates verbose output to standard output. 

0

(zero) Store without using ZIP compression. 

M

Do not create a manifest file entry (for c and u), or delete a manifest file entry if one exists (for u). 

m

Includes namevalue attribute pairs from the specified manifest file manifest in the file at META-INF/MANIFEST.MF. A namevalue pair is added unless one already exists with the same name, in which case its value is updated. 

On the command line, the letters m and f must appear in the same order that manifest and jarfile appear. 

-C  dir

Temporarily changes directories (cd dir) during execution of the jar command while processing the following inputfiles argument. Its operation is intended to be similar to the -C option of the UNIX tar utility. 

-Joption

Pass option to the Java runtime environment, where option is one of the options described on the reference page for the java application launcher. For example, -J-Xmx48M sets the maximum memory to 48 megabytes. It is a common convention for -J to pass options to the underlying runtime environment.

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值