Today we will learn how to run JAR File in java. In last tutorial we learned how to create jar file. Here we will learn how to run jar file from command line and some other aspects of using jar file.
How to run JAR File
We can run a jar file using java command but it requires
Main-Class entry in jar manifest file.
Main-Class is the entry point of the jar and used by java command to execute the class. My project structure looks like below image.
I have to add manifest file in jar file, so I will create a manifest file with below details.
Now we will use jar command to create jar file with our manifest file data.
pankaj@JD:~/CODE/MyProject/bin$ jar cvfm MyProject.jar manifest.txt com added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/journaldev/(in = 0) (out= 0)(stored 0%) adding: com/journaldev/test/(in = 0) (out= 0)(stored 0%) adding: com/journaldev/test/MyTest.class(in = 444) (out= 303)(deflated 31%) adding: com/journaldev/util/(in = 0) (out= 0)(stored 0%) adding: com/journaldev/util/MyUtil.class(in = 444) (out= 304)(deflated 31%)
Now when I unpack and check the contents of MANIFEST.MF file, it contains following data.
Manifest-Version: 1.0 Created-By: 1.6.0_37 (Apple Inc.) Main-Class: com.journaldev.util.MyUtil
Manifest-Version and Created-By entries are added by jar command. Now we are ready to run jar file through command line.
Run Jar File from command line
pankaj@JD:~/CODE/MyProject/bin$ java -jar MyProject.jar MyUtil main method
So it’s executed MyUtil main method. That’s great when we have single class with main method.
What if my project has multiple entry points and I want to change the entry point without creating the jar again. So we can use the jar command to update the manifest file.
For updating a file using jar command, file structure should be similar otherwise it will add new file to another directory. Since manifest file is located at
META-INF/MANIFEST.MF. We will rename the manifest.txt to MANIFEST.MF and put it inside META-INF directory. Then run the below command to update the entry point.
pankaj@JD:~/CODE/MyProject/bin$ jar uvfm MyProject.jar META-INF/MANIFEST.MF Jan 30, 2013 5:40:27 PM java.util.jar.Attributes read WARNING: Duplicate name in Manifest: Main-Class. Ensure that the manifest does not have duplicate entries, and that blank lines separate individual sections in both your manifest and in the META-INF/MANIFEST.MF entry in the jar file. updated manifest
Note that warning is because of duplicate entries and manifest file can have only one entry for each meta property, but it still updates the entry point by updating the manifest file. Now when you will run the jar, it will execute the changed entry class.
What if we don’t want to change the entry point but want to execute another class from jar file. It seems confusing but solution is real simple. All you need to do it add the jar to class path and then execute the class like a normal java class with main method.
pankaj@JD:~/CODE/MyProject/bin$ java -cp MyProject.jar com.journaldev.util.MyUtil MyUtil main method
That’s all for how to run jar file in java with single entry point, different entry points and without any entry point at all using java classpath.
Reference: Oracle Doc