how to create JAR File [closed]

别说谁变了你拦得住时间么 提交于 2020-01-05 18:53:26

问题


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.


回答1:


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>



回答2:


Unfortunately Eclipse does not have a command line interface allowing you to do the same things from a command line, as you can do inside Eclipse.

In my experience the most reliable approach is to learn Maven and then adapt your current code to Maven conventions (possibly split over several modules). This has the added benefit of your project being usable in all the current three major IDE's instead of only Eclipse.



来源:https://stackoverflow.com/questions/24346053/how-to-create-jar-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!