How to do Eclipse's "export jar" from the command line

15,448

To manually create a jar file, you can use the "jar" utility. A jar file is basically an archive file (in fact, you can use any zip tool to extract the jar contents). To create a jar file, you specify the "c" option and all the .class files that you compiled and that you want included. The jar command-line mimics the tar command. So, for example, to add your class files whose root folder is com, you type:

  jar cf test.jar com

Optionally, you can add manifest data in the form of a manifest file to specify a default executable and other information.

However, a simpler way to build a jar file is to just use ant.

Checkout this tutorial from the ant website, which gives an example of a typical ant build file: http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html

Share:
15,448
lacker
Author by

lacker

Updated on June 14, 2022

Comments

  • lacker
    lacker almost 2 years

    In my workflow for creating a jar to distribute my code, I currently:

    1. right-click on my project in Eclipse
    2. select "Export"
    3. select "JAR file"
    4. uncheck the top-level files like .classpath, .project
    5. check only "export generated class files"
    6. click "finish"

    This ensures the .class files are up-to-date and creates the jar. I would like to do this same thing from the command line, but most documentation for creating jars seems to be just jarring up files that already exist, without the phase of creating the .class files. How can I do this from the command line?

  • lacker
    lacker over 12 years
    I know how to use jar, but this workflow creates the .class files and then jars them up. creating the .class files is the trickier part.
  • hopia
    hopia over 12 years
    The .class files are built by the javac compiler. Ant also makes it easy to do this (through the javac task). I would suggest you go through the ant tutorial. Unless there's a reason you don't want to use ant? Ant is also typically what you would use if you want to automate your build, unit test, and release process.
  • lacker
    lacker over 12 years
    i just didnt want to add ant to our process because it's an android library. but looking over android stuff it seems like ant is the way to go. ok so I'll just do ant. thanks for your explanation.
  • hopia
    hopia over 12 years
    I see what you mean. I didn't see the android tag. For android, you need to generate a build script using the tools in the android sdk. Yes, it is more tricky to do so but this is a common problem. Here's one tutorial that walks you through generating the build script: androidengineer.com/2010/06/…
  • Lisitso
    Lisitso over 8 years
    Unfortunately the tutorial suggested contains errors. For example the command "jar cfm build\jar\HelloWorld.jar myManifest -C build\classes ." doesn't generate a valid jar because of there are missing information in the manifest of the jar.
  • Jesse Chisholm
    Jesse Chisholm over 7 years
    re: Unless there's a reason you don't want to use ant? My own take on the OP is I'm using Eclipse IDE, I rather use Eclipse from command line. So, using javac and ant are kind of outside the scope of the OP. That said, see stackoverflow.com/questions/206473/… for the eclipse command line way to replace javac. I have yet to find the right command for eclipse export jar from the command line.