For years I have been using an IDE (Eclipse) to compile my jar files for me, through the years I have learned about how they work however I still don't fully understand how the jar knows where the main method is, I am also curious about how simple (or not) it is to compile one manually.
I have a (sort of) IDE I'm working on that will need to be able to compile and run a jar that includes both the file from the user and either a jar or a bunch of other classes (the API), I have seen some questions here mentioning Java JavaCompiler
class but never giving demo code and there seems to be a next to no one that knows how to compile manually so I would like to contribute. So, how can I create a jar file using java code? Please provide demo code.

- 775
- 6
- 22
-
Here .. http://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file – vidit Jul 18 '13 at 22:23
-
"In Eclipse you can do it simply as follows :", "Compile your classes." - not what I am asking, "how can I create a jar file using java code?" – Lee Fogg Jul 18 '13 at 23:10
-
Java includes a fully functional set of zip file utilities (java.util.zip) that can be used to create jar files. And there are some additional "nice to have" facilities in java.util.jar. – Hot Licks Jul 18 '13 at 23:59
-
@LeeAllan- Did you look at the accepted answer on that question? It tells you exactly that and with a code example. – vidit Jul 19 '13 at 01:12
-
The only java code in that example is the file to be compiled, I need the code in which will compile .java files into a runnable jar. – Lee Fogg Jul 19 '13 at 01:31
-
Have you check out the explanation here: http://stackoverflow.com/questions/2946338/how-do-i-programmatically-compile-and-instantiate-a-java-class Then you can take a look at how to build JAR programmatically: http://docs.oracle.com/javase/6/docs/api/java/util/jar/JarOutputStream.html – user2507946 Jul 19 '13 at 03:03
1 Answers
I still don't fully understand how the jar knows where the main method is
That's the job of the manifest file.
I am also curious about how simple (or not) it is to compile one manually.
It's pretty straightforward - you use the jar
tool after you've built the class files.
Let's do a full walk through.
Create a directory called src
and a directory called bin
. Under src
, create a directory demo
and a file called Test.java
in that directory:
package demo;
public class Test {
public static void main(String[] args) {
System.out.println("Working!");
}
}
Now compile the code:
javac -d bin src/demo/Test.java
(That will work on both Unix and Windows.)
Then create a manifest file called manifest.txt
- it doesn't matter where it goes really, but I'll just keep it in src
for the moment:
Main-Class: demo.Test
Now build a jar file:
jar cfm test.jar src\manifest.txt -C bin demo/Test.class
And run it:
java -jar test.jar
These days you can specify the entry point on the command line instead of building a manifest file yourself:
jar cfe test.jar demo.Test -C bin demo/Test.class
See the linked docs for more details on how to use the jar
tool, and the potential contents of the manifest.

- 1,421,763
- 867
- 9,128
- 9,194
-
My IDE is written in Java and needs to compile for the user as explained. Could I get Java to run these Command Prompt commands? Or even if not create the necessary batch file? – Lee Fogg Jul 18 '13 at 23:18
-
@LeeAllan: You *could*, but you'd be a lot better off using the JavaCompiler class you linked to. Likewise you can use the classes in `java.util.jar` to build jar files programmatically. – Jon Skeet Jul 19 '13 at 05:44