How to do Eclipse's "export jar" from the command line
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
lacker
Updated on June 14, 2022Comments
-
lacker almost 2 years
In my workflow for creating a jar to distribute my code, I currently:
- right-click on my project in Eclipse
- select "Export"
- select "JAR file"
- uncheck the top-level files like .classpath, .project
- check only "export generated class files"
- 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 over 12 yearsI 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 over 12 yearsThe .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 over 12 yearsi 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 over 12 yearsI 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 over 8 yearsUnfortunately 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 over 7 yearsre:
Unless there's a reason you don't want to use ant?
My own take on the OP isI'm using Eclipse IDE, I rather use Eclipse from command line.
So, usingjavac
andant
are kind of outside the scope of the OP. That said, see stackoverflow.com/questions/206473/… for the eclipse command line way to replacejavac
. I have yet to find the right command foreclipse export jar
from the command line.