How to run a Jar file
Steps to run a JAR file on Windows, Mac or Linux
To run a JAR file on Windows, Mac or Ubuntu machines, follow these steps:
- Verify that Java is installed on your computer
- Confirm the computer’s PATH variable includes Java’s \bin directory
- Double-click the JAR file if auto-run has been configured
- Run the JAR file on the command line or terminal window if a double-clicking fails
If you do not have Java installed, and the PATH variable is not set correctly, attempts to run a JAR file on Windows or Ubuntu will result in a ‘Java not recognized‘ error.
Why does double-clicking not run my JAR file?
Some Java installers configure Windows or Ubuntu to run Java JAR files when they are double clicked, but not every installer supports this configuration.
Furthermore, it is possible that you didn’t select the JAR double-click functionality during the Java install.
Is every JAR file executable?
Keep in mind that not every JAR file is runnable. Some JAR files are just support files intended to be used by other Java apps.
For a JAR file to run, the JAR itself must contain at least one class that has a runnable main method.
Furthermore, the class with the main method must be listed as the main-class in the JAR’s manifest file. If not, the class must be explicitly stated on the command line when the JAR is run.
How do you run a JAR at the command line?
As long as you have Java installed, and the PATH variable points to the \bin directory of the JDK, you issue the following command to run a JAR file at the command line:
java -jar jarfilename.jar
This Java JAR run command assumes the JAR in question is located in the current folder. If the JAR file to run is located in a different folder, you’ll need to provide a full path to the file.
|Function||Java JAR command|
|Run a JAR file||jar -jar jarfilename.jar|
|Create a JAR file||jar cf jarfiletocreate.jar file-list|
|Open a JAR file||jar xf jar-file-to-open.jar|
|Run a JAR without a manifest||java -cp jarfilename.jar com.example.MainClass|
What is inside a JAR file?
JAR files are simply ZIP files that contain all of the resources required to run a Java application.
A JAR file typically contains:
- Java source files
- Property files
- XML files
- Image files
- A manifest file
What does JAR stand for?
The term JAR is an acronym built from the words: Java Application aRchive.
The term ‘archive’ is really key to understanding what a JAR file is. The JAR file encompasses every file required to support an application, zipped up and compressed into a single, archive file.
The Java JAR file uses a standard compression algorithm used by all of the most popular tools. A Java JAR file is really just a ZIP file with a .jar extension.
How do I extract the contents of a JAR file?
If you look inside at its contents, you can open a JAR file and extract its contents with any standard ZIP or decompression utility.
Popular JAR file opener tools include:
- Windows Extractor
You can also open a JAR file in Windows PowerShell or an Ubuntu Terminal window with the following command if you have the JDK installed:
jar xf name-of-jar-file.jar [files-to-open]
Why do I get a ‘Could not load main class’ error when I run my JAR?
Not all JAR files are meant to run. Some are just standard libraries with no execution capabilities at all.
If you try to execute a JAR file that is not meant to be run, you will get an error message that says:
Java JAR error: Could not find or load main class ...
Could not load main class fix
If you get this error when you run a JAR that should be executable, the problem could be one of these issues:
- The main-class to run was not listed in the runnable JAR file’s manifest file;
- The JAR’s manifest file is missing; or
- The JAR file distributor wants you to specify the main-class on the command line.
If you know the Java class that should act as the entry point of a runnable JAR, you can explicitly specify that file at the command line as follows:
java -cp jarfilename.jar com.example.EntryPointClass
Notice the flag to run the JAR file has changed from
When the main-class is not specified in the manifest file, you must use the classpath flag (
-cp), not the
Running executable Java JAR files
JAR files are an excellent way to distribute and deploy cross-platform applications written in Java.
To run JAR files, first make sure you have Java installed, your environment is properly configured, and the JAR file’s manifest file lists the application’s entry-point. With all of these puzzle pieces in place, your Java JAR file will run without issues.