JBoy
JBoy

Reputation: 5755

what the java command's -jar option really does

Does the -jar option of the java command also compile the sources before running the main method? I believe so but i would like to have a better understanding of the internal process, from the man page you can clearly see a small workflow sequence:

-jar
             Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a  line  of
             the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and
             the Jar trail of the Java Tutorial @

But it does not mention that it compiles the sources.

Upvotes: 0

Views: 875

Answers (2)

Mustafa sabir
Mustafa sabir

Reputation: 4360

The description you posted itself says everything

Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a  line  of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java 

Jar files contain a manifest file which has a line of form Main-Class: classname. Here the class that contains the main method is given. Its important because standalone application in java begin execution at main method.

Command to run the application packaged in jar looks like :-

java -jar jar-file 

jar-file is name of the jar.

Edit:- And to explicitly answer your question, NO , it does not compile the source code. The jar itself contains compiled class files. You can create jar by addin class files as

jar cvf Count.jar Count.class Sum.class

jar-command

cvf-options check details here http://docs.oracle.com/javase/tutorial/deployment/jar/build.html

Count.class Sum.class - name of class files(compiled source code) space separated to add in the jar.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503649

Does the -jar option of the java command also compile the sources before running the main method?

No, absolutely not. It just specifies the jar file in which to find the manifest specifying the main class, and (usually) the class file for that class. It definitely doesn't compile anything.

Upvotes: 2

Related Questions