How to run JAR File With Examples

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.

Now when I unpack and check the contents of MANIFEST.MF file, it contains following data.

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

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.

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.

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

By admin

Leave a Reply

%d bloggers like this: