编程小贴士

给你的编程提供小点子


jar命令和jar包详解

Jar的定义

首先套用百度百科对Jar的解释:Jar(Java Archive,Java 归档文件)是与平台无关的文件格式,它允许将许多文件组合成一个压缩文件。为 J2EE 应用程序创建的 Jar文件是 Ear 文件(企业 Jar 文件)。Jar 文件不仅用于压缩和发布,而且还用于部署和封装库、组件和插件程序,并可被像编译器和 JVM 这样的工具直接使用。在 Jar 中包含特殊的文件,如manifests和部署描述符,用来指示工具如何处理特定的 Jar。

Jar 文件格式提供了许多优势和功能,其中很多是传统的压缩格式如 Zip所没有提供的。它们包括:

² 安全性可以对文件内容加上数字化签名。这样,能够识别签名的工具就可以有选择地为您授予软件安全特权,这是其他文件做不到的,它还可以检测代码是否被篡改过。

²  减少下载时间      如果一个 applet 捆绑到一个 Jar 文件中,那么浏览器就可以在一个 HTTP 事务中下载这个 applet 的类文件和相关的资源,而不是对每一个文件打开一个新连接。

²  压缩        Jar 格式允许您压缩文件以提高存储效率。

²  传输平台扩展      Java 扩展框架 (Java Extensions Framework) 提供了向 Java 核心平台添加功能的方法,这些扩展是用 Jar文件打包的。

² 包密封   存储在 Jar 文件中的包可以选择进行 密封,以增强版本一致性和安全性。密封一个包意味着包中的所有类都必须在同一 Jar 文件中找到。

²  包版本控制      一个 Jar 文件可以包含有关它所包含的文件的数据,如厂商和版本信息。

²  可移植性        处理 JAR 文件的机制是 Java 平台核心 API 的标准部分。

 

Jar的文件结构

Jar文件的结构是什么样的呢,让我们看一下Java运行时的基础类库rt.jar的解压后截图:

jar命令和jar包详解

Jar文件与 Zip文件唯一的区别就是在 Jar 文件的内容中,包含了一个 META-INF/MANIFEST.MF 文件,这个文件是在生成 Jar 文件的时候自动创建的。如上图中的2号区域。里面多了一个MANIFEST.MF文件,这个文件描述了该Jar的很多作息(包括Jar文件的入口类、类路径和版本等),下图是ant.jar的MANIFEST.MF文件的内容截图

2014-06-29_05-14-33.png

Jar命令的用法

Jar命令相信对于Java攻城狮而言应该不陌生。

  1. 创建Jar包并显示打包过程        jar cvf Jar文件名称需要打包的目录或是文件

我本地项目simplecd截图如下:

jar命令和jar包详解

我想把src下面的所有文件打成一个名为simplecd的Jar包,运行cmd进入编译后的目录F:\workspace\simplecd\WebContent\WEB-INF\classes,执行jar cvf simplecd.jar .

注意最后面的“.”代表当前文件夹,如果有子目录,它会对其进行递归处理。

2、显示指定Jar包中的内容,即Jar包中的文件列表,用上面生成的simplecd.jar包来做实验,运行jar vtf simplecd.jar,

3、解压Jar包,当然我们也可以用解压缩工具如winrar来解压。但是如果我们电脑上没有装呢,那我们就可以用:jar xvf simplecd.jar

如果我们只想提取Jar包的某一个文件,那么我们可以运行jar xf simplecd.jar Test.class

4、往已知Jar包中添加文件,当我们需要往Jar包中添加一些新的功能时,可以把新编译后的文件加进原来的 Jar包,

我们可以进入刚才打包好的Jar包目录,运行: jar uvf simplecd.jar -C F:\workspace\test\bin .

5、创建一个Jar索引(JarIndex)Jar i simplecd.jar运行以后,我们就可以在simplecd.jar中的目录META-INF下面找到一个INDEX.LIST

jar命令和jar包详解

里面的内容如下(包含每个已索引的 Jar 文件中包含的包或者类的名字):

jar命令和jar包详解

那么JarIndex出现的意义是什么呢?它有什么作用呢?

网上找了一个牛人的解释:

如果一个应用程序或者 applet 捆绑到多个 Jar文件中,那么类装载器就使用一个简单的线性搜索算法搜索类路径中的每一个元素,这使类装载器可能要下载并打开许多个 Jar 文件,直到找到所要的类或者资源。如果类装载器试图寻找一个不存在的资源,那么在应用程序或者 applet 中的所有 Jar文件都会下载。对于大型的网络应用程序和 applet,这会导致启动缓慢、响应迟缓并浪费带宽。

从 JDK 1.3 以后,Jar文件格式开始支持索引以优化网络应用程序中类的搜索过程,特别是 applet。JarIndex 机制收集在 applet 或者应用程序中定义的所有 JAR 文件的内容,并将这些信息存储到第一个 JAR 文件中的索引文件中。下载了第一个 Jar文件后,applet 类装载器将使用收集的内容信息高效地装载 Jar 文件。这个目录信息存储在根 Jar文件的 META-INF 目录中的一个名为 INDEX.LIST 的简单文本文件中。

6、创建执行的Jar文件,如果我们要把Jar包打成可执行的话,得指定Main-Class即运行时的主类,拿上面项目为例,运行

jar cvfme simplecd.jar manifest.txt me.simplecd.SimpleCD  -C F:\workspace\simplecd\WebContent\WEB-INF\classes .

运行完成后,在C盘根目录下面就生成了simplecd.jar文件

接下来我们可以运行命令:java –jar simplecd.jar来运行这个jar包:我们看到指定的SimpleCD类正在运行。

158 Responses to “ jar命令和jar包详解 ”

  1. best pron说道:

    V2qudW You completed various nice points there. I did a search on the topic and found mainly people will go along with with your blog.

  2. This particular blog is obviously educating and factual. I have picked up a bunch of useful advices out of this amazing blog. I ad love to return again soon. Thanks a lot!

  3. Ultimately, an issue that I am passionate about. I have looked for information of this caliber for the very last numerous hours. Your site is greatly appreciated.

  4. drawing for kids说道:

    I really liked your blog article.Really thank you! Really Cool.

  5. This particular blog is really interesting as well as factual. I have chosen helluva helpful things out of this source. I ad love to return every once in a while. Thanks a lot!

  6. Smokers Lungs说道:

    Very good post. I absolutely love this site. Stick with it!

  7. This is very interesting, You are a very skilled blogger. I ave joined your feed and look forward to seeking more of your excellent post. Also, I have shared your site in my social networks!

  8. spelling on several of your posts. A number of them are rife

  9. Thanks again for the article.Thanks Again. Awesome.

  10. Oluwadamilare说道:

    Of course, what a magnificent site and instructive posts, I will bookmark your site.Best Regards!

  11. Is not it superb any time you get a fantastic submit? Value the admission you given.. Fantastic opinions you might have here.. Truly appreciate the blog you provided..

  12. Thanks-a-mundo for the blog post.Thanks Again.

  13. This is my first time pay a quick visit at here and i am in fact pleassant to read all at one place.

  14. site说道:

    stiri interesante si utile postate pe blogul dumneavoastra. dar ca si o paranteza , ce parere aveti de cazarea la particulari ?.

  15. There as definately a great deal to know about this issue. I like all of the points you have made.

  16. Thank you for your article post.Really thank you! Really Cool.

  17. Usually I don at read article on blogs, however I would like to say that this write-up very compelled me to check out and do so! Your writing taste has been amazed me. Thanks, quite nice article.

  18. go nhua da nang说道:

    Ton avenir selon la cartomancie elle horoscope semaine

  19. Oakley dIspatch Sunglasses Appreciation to my father who shared with me regarding this webpage, this web site is in fact awesome.

  20. pre booked说道:

    Very nice post. I certainly love this site. Continue the good work!

  21. gia sat thep说道:

    is the greatest. my homepage Uggs Cyber Monday Deals

  22. Really enjoyed this blog.Really thank you! Will read on

  23. This is a topic which is close to my heart Thank you! Exactly where are your contact details though?

  24. so much time I had spent for this information!

  25. Im thankful for the blog.Thanks Again. Want more.

  26. Share说道:

    This unique blog is obviously entertaining additionally diverting. I have discovered a bunch of useful things out of this blog. I ad love to go back every once in a while. Thanks a bunch!

  27. great site说道:

    seeing very good gains. If you know of any please share.

  28. Nwokolo说道:

    It as not that I want to duplicate your web-site, but I really like the pattern. Could you tell me which design are you using? Or was it custom made?

  29. klm coupon code说道:

    I truly enjoy looking through on this web site, it has got superb posts. аЂа‹One should die proudly when it is no longer possible to live proudly.аЂа› by Friedrich Wilhelm Nietzsche.

  30. Your method of explaining all in this piece of writing is truly good, all be able to simply be aware of it, Thanks a lot.

  31. Im thankful for the article post.Really thank you! Will read on

  32. Justin Bieber说道:

    pretty beneficial stuff, overall I believe this is really worth a bookmark, thanks

  33. plumbing can actually be a hardwork specially if you usually are not very skillfull in undertaking residence plumbing::

  34. Wow, what a video it is! Truly good feature video, the lesson given in this video is really informative.

  35. Of course, what a fantastic blog and instructive posts, I surely will bookmark your site.Best Regards!

  36. Just wanna admit that this is handy , Thanks for taking your time to write this.

  37. Thanks so much for the article post.Really looking forward to read more. Awesome.

  38. It as not that I want to copy your web site, but I really like the layout. Could you let me know which design are you using? Or was it tailor made?

  39. giay da nam说道:

    I think this is a real great blog post.Thanks Again. Keep writing.

  40. Regards for this post, I am a big big fan of this website would like to go on updated.

  41. sua hat huu co说道:

    Usually I don at learn post on blogs, but I wish to say that this write-up very pressured me to take a look at and do it! Your writing taste has been surprised me. Thanks, quite nice post.

  42. Thanks-a-mundo for the post. Really Great.

  43. There is certainly a great deal to find out about this subject. I like all the points you ave made.

  44. Miracle Ndu说道:

    It as hard to come by knowledgeable people on this subject, but you seem like you know what you are talking about! Thanks

  45. this website说道:

    You made some clear points there. I did a search on the issue and found most individuals will agree with your website.

  46. seo tips说道:

    tаАаб‚Т€Т“me now and finallаАааБТ“ got the braveаА аБТ–y

  47. healthy说道:

    It as not that I want to replicate your web-site, but I really like the style. Could you let me know which theme are you using? Or was it custom made?

  48. I value the post.Much thanks again. Awesome.

  49. Hosting说道:

    superb post.Ne aer knew this, appreciate it for letting me know.

  50. Thank you ever so for you blog.Really thank you! Really Cool.

  51. There is visibly a bunch to realize about this. I feel you made various nice points in features also.

  52. wow, awesome post.Thanks Again. Fantastic.

  53. Looking forward to reading more. Great blog.Really thank you! Will read on…

  54. Blusa Tejidas说道:

    It is actually a nice and helpful piece of info. I am happy that you simply shared this useful info with us. Please keep us up to date like this. Thanks for sharing.

  55. Arctic Circle说道:

    start my own blog (well, almostHaHa!) Wonderful job.

  56. try this说道:

    Thank you for your blog article. Really Great.

  57. white shoes说道:

    Very informative blog article.Much thanks again. Awesome.

  58. value说道:

    items, but still flexible enough to fish vs

  59. There is noticeably a bundle to learn about this. I assume you made sure nice factors in features also.

  60. Waec说道:

    louis vuitton handbags louis vuitton handbags

  61. I was really confused, and this answered all my questions.

  62. You will require to invest a substantial quantity

  63. types of love说道:

    Wonderful article! We will be linking to this particularly great post on our site. Keep up the good writing.

  64. Some really nice and also utilitarian information on this website, too I think the format has fantastic features.

  65. woh I am pleased to find this website through google.

  66. Seriously.. thanks for starting this up. This web

  67. Thanks-a-mundo for the blog post.Much thanks again. Great.

  68. Major thankies for the blog article. Keep writing.

  69. scary maze说道:

    IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžd ought to seek advice from you here. Which is not something I do! I love reading an article that could make individuals feel. Also, several thanks permitting me to comment!

  70. Wow, great blog.Really looking forward to read more. Keep writing.

  71. cris urzua说道:

    There is apparently a bunch to realize about this. I believe you made certain nice points in features also.

  72. tips de ventas说道:

    Thanks for sharing, this is a fantastic article.Thanks Again. Great.

  73. travel说道:

    You made some clear points there. I did a search on the subject matter and found most people will approve with your site.

  74. Wow! This could be one particular of the most useful blogs We ave ever arrive across on this subject. Actually Excellent. I am also an expert in this topic therefore I can understand your effort.

  75. Major thankies for the article.Really thank you! Really Great.

  76. Of course, what a magnificent website and informative posts, I will bookmark your blog.Best Regards!

  77. If you need to age well, always be certain to understand something new. Learning is essential at every stage of life.

  78. Some genuinely choice blog posts on this site, saved to bookmarks.

  79. pretty helpful material, overall I think this is worth a bookmark, thanks

  80. Thank you for your blog article.Really thank you! Fantastic.

  81. I will immediately seize your rss feed as I can not to find your email subscription hyperlink or newsletter service. Do you ave any? Kindly allow me realize so that I could subscribe. Thanks.

  82. Wonderful, what a webpage it is! This website gives useful information to us, keep it up.

  83. know more说道:

    Regards for helping out, superb information. The surest way to be deceived is to think oneself cleverer than the others. by La Rochefoucauld.

  84. to read more说道:

    This particular blog is really interesting and also amusing. I have chosen many handy tips out of this blog. I ad love to come back again and again. Thanks a lot!

  85. plumbing can really plumbing can really be a hardwork specially if you are not very skillfull in doing home plumbing.,

  86. more info说道:

    This unique blog is no doubt entertaining and also amusing. I have discovered a lot of handy advices out of this source. I ad love to visit it again and again. Thanks a lot!

  87. Some genuinely superb content on this site, regards for contribution.

  88. this website说道:

    Wow! This could be one particular of the most useful blogs We ave ever arrive across on this subject. Basically Fantastic. I am also a specialist in this topic therefore I can understand your effort.

  89. Some genuinely nice stuff on this website , I enjoy it.

  90. for details说道:

    What as up, just wanted to mention, I liked this blog post. It was funny. Keep on posting!

  91. Very nice article, exactly what I needed.

  92. I think this is a real great blog post.Really looking forward to read more. Will read on…

  93. Very good post. I absolutely love this site. Continue the good work!

  94. This particular blog is without a doubt interesting additionally diverting. I have picked many interesting stuff out of this blog. I ad love to return again and again. Cheers!

  95. Really informative blog article. Keep writing.

  96. Nnamdi说道:

    Say, you got a nice blog post.Much thanks again.

  97. see this website说道:

    I went over this site and I think you have a lot of fantastic information, bookmarked (:.

  98. Just desire to say your article is as surprising.

  99. usually posts some pretty interesting stuff like this. If you

  100. app说道:

    Rattling clear site, thankyou for this post.

  101. Wow, great post.Really looking forward to read more. Will read on

  102. browse说道:

    This is a topic that as close to my heart Best wishes! Exactly where are your contact details though?

  103. to read more说道:

    Thanks for the post.Really thank you! Awesome.

  104. It as genuinely very complex in this busy life to listen news on TV, thus I only use internet for that purpose, and get the most up-to-date news.

  105. visit website说道:

    Your style is so unique in comparison to other people I have read stuff from. Thank you for posting when you ave got the opportunity, Guess I all just book mark this blog.

  106. buy说道:

    Some really good info , Glad I found this.

  107. This site truly has all the info I needed concerning this subject and didn at know who to ask.

  108. My brother suggested I might like this website. He was totally right. This post truly made my day. You cann at imagine simply how much time I had spent for this information! Thanks!

  109. buat akun pb说道:

    Major thankies for the blog article.Really thank you! Keep writing.

  110. more info说道:

    Right now it looks like Expression Engine is the preferred blogging platform out there right now. (from what I ave read) Is that what you are using on your blog?

  111. It as hard to find educated people about this subject, however, you seem like you know what you are talking about! Thanks

  112. MIAMI SEO说道:

    Really appreciate you sharing this blog.Really looking forward to read more. Want more.

  113. Very good blog article.Really thank you! Will read on

  114. good day说道:

    Very nice info and straight to the point. I don at know if this is truly the best place to ask but do you people have any ideea where to get some professional writers? Thanks in advance

  115. I was recommended this website by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are amazing! Thanks!

  116. Just imagined I might remark and say fantastic concept, did you help it become on your individual? Seems to be really fantastic!

  117. Thanks again for the article post.Really looking forward to read more. Cool. this link

  118. I was recommended this web site by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are amazing! Thanks!

  119. flycam gia re说道:

    very good post, i actually love this web site, keep on it

  120. south korea jersey ??????30????????????????5??????????????? | ????????

  121. Just a smiling visitant here to share the enjoy (:, btw outstanding style.

  122. Wow, great blog.Really looking forward to read more. Will read on

  123. very nice put up, i definitely love this website, keep on it

  124. Game Online说道:

    Thanks-a-mundo for the blog article.Really looking forward to read more. Really Great.

  125. Canon dslr 80d说道:

    It as hard to come by knowledgeable people on this topic, however, you seem like you know what you are talking about! Thanks

  126. Your style is so unique compared to other people I ave read stuff from. I appreciate you for posting when you have the opportunity, Guess I all just book mark this page.

  127. Thanks for the post.Really thank you! Fantastic.

  128. Thanks-a-mundo for the article post. Great.

  129. hand craft说道:

    Looking around I like to surf around the online world, often I will just go to Stumble Upon and follow thru

  130. Really appreciate you sharing this blog. Will read on

  131. I think you have observed some very interesting points, regards for the post.

  132. Well I definitely enjoyed studying it. This post procured by you is very constructive for correct planning.

  133. I truly appreciate this post.Really looking forward to read more. Really Cool.

  134. casa de trofeos说道:

    It?s actually a cool and useful piece of information. I?m satisfied that you just shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

  135. Thank you ever so for you blog post.Really looking forward to read more. Really Cool.

  136. Zebra说道:

    Thanks again for the blog article.Really looking forward to read more.

  137. to learn more说道:

    other hand I will certainly come again again.

  138. I truly appreciate this article.Really looking forward to read more. Will read on

  139. Really enjoyed this article.Thanks Again. Really Cool.

  140. Utterly written subject matter, thankyou for entropy.

  141. kinogrut说道:

    I truly appreciate this post.Really looking forward to read more. Awesome.

  142. I simply could not depart your website prior to suggesting that I extremely enjoyed the standard info an individual supply on your guests? Is gonna be back frequently in order to inspect new posts

  143. KS说道:

    I reckon something really interesting about your web blog so I saved to my bookmarks.

  144. of hardcore SEO professionals and their dedication to the project

  145. I really liked your blog post.Much thanks again. Keep writing.

  146. mealcoupons说道:

    Thank you for your blog.Really looking forward to read more. Great.

  147. me suba说道:

    L8L1ys Muchos Gracias for your article.Really thank you! Really Cool.

  148. arcadia realtor说道:

    Way cool! Some very valid points! I appreciate you penning this write-up and the rest of the site is extremely good.

  149. Thanks again for the article.Thanks Again. Much obliged.

  150. This site was how do I say it? Relevant!! Finally I ave found something which helped me. Cheers!

  151. This is a good tip especially to those new to the blogosphere. Simple but very accurate info Thank you for sharing this one. A must read article!

  152. Thank you ever so for you blog.Really thank you! Great.

  153. I wanted to thank you for this fantastic article, I certainly loved each and every small bit of it. I ave bookmarked your web site to look at the newest stuff you post.

  154. Many thanks for sharing this first-class post. Very inspiring! (as always, btw)

  155. know more说道:

    some times its a pain in the ass to read what people wrote but this internet site is rattling user pleasant!.

  156. There are positively a couple extra details to assume keen on consideration, except gratitude for sharing this info.

  157. Your explanation is the best I have seen so far

  158. Wow, great article.Really looking forward to read more. Really Cool.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>