如何创建JAR文件 [英] how to create JAR File

查看:136
本文介绍了如何创建JAR文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道创建JAR文件很简单,只需使用eclipse,右键单击项目并选择导出并按照向导。



还有另一种方式是命令行,我们可以使用这个命令。

  jar cf myjarfilename.jar file1.class file2.class 

问题是我无法使用eclipse创建JAR,因为我的代码是在单独的项目中编译的,然后将.class文件放在一个特定的层次结构中,编译成JAR并放置在我的网络应用程序使用它的特定位置。



我可以使用命令行,但这不实用,因为我的层次结构是复杂的使用了大量的.class文件。 (文件夹和只有.class文件)



现在我需要一个JAR。

解决方案

是的,制作.jar文件很容易。



1)Eclipse:
选择项目>右键单击,导出为.jar



2).bat文件/ shell脚本:

  jar cf myjarfilename.jar file1.class file2 .class 

3).bat文件/ shell脚本:

  mkdir staging-directory 
copy / s source-files staging-directory
jar cf myjarfilename.jar staging-directory

4)写一个 Ant build.xml Maven pom.xml 自动化您的构建(包括生成.jar文件)。



这是一个非常好的Apache Ant / build.xml教程:



http://www.vogella.com/tutorials/ApacheAnt/article.html

 <! - 创建可部署的jar文件 - > 
< target name =jardepends =compile>
< jar destfile =$ {dist.dir} \de.vogella.build.test.ant.jarbasedir =$ {build.dir}>
< manifest>
< attribute name =Main-Classvalue =test.Main/>
< / manifest>
< / jar>
< / target>


I know creating JAR files is easy, just use eclipse, right click on the project and select export and follow the wizard.

There is also another way that is command-line, we can use this command.

jar cf myjarfilename.jar file1.class file2.class

The problem is I cannot use use eclipse to create JAR because my code is compiled in separate projects and then I place .class files in a particular hierarchy, compiled 'em into a JAR and place 'em at a specific location from where my web-app uses it.

I could use the command line but that is not practical as my hierarchy is complex and uses a lot of .class files. (folders and only .class files)

Now I need to make a JAR out of it. How to.

解决方案

Yes, making a .jar file is easy.

There are many alternatives.

1) Eclipse: Select project > Right-click, Export as, .jar

2) .bat file/shell script:

jar cf myjarfilename.jar file1.class file2.class

3) .bat file/shell script:

mkdir staging-directory
copy/s source-files staging-directory
jar cf myjarfilename.jar staging-directory

4) Write an Ant build.xml or Maven pom.xml to automate your build (including generating your .jar file).

Here's a very good tutorial on Apache Ant/build.xml:

http://www.vogella.com/tutorials/ApacheAnt/article.html

  <!--Creates the deployable jar file  -->
  <target name="jar" depends="compile">
    <jar destfile="${dist.dir}\de.vogella.build.test.ant.jar" basedir="${build.dir}">
      <manifest>
        <attribute name="Main-Class" value="test.Main" />
      </manifest>
    </jar>
  </target>

这篇关于如何创建JAR文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆