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:

  1. Verify that Java is installed on your computer
  2. Confirm the computer’s PATH variable includes Java’s \bin directory
  3. Double-click the JAR file if auto-run has been configured
  4. 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.

Run JARs in the Java JDK

To run a JAR file, the Java JDK or JRE must be installed on your computer.

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, there is a possibility that the JAR double-click functionality was not a selected option 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.

Not every JAR file will run.

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, a full path to the file is required.

Commands to Create, Extract and Run JAR files
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 will typically contain:

  • 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 is 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 would 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:

  • WinZip
  • Windows Extractor
  • Gzip
  • WinRAR
  • 7-Zip
Run and open JAR file

You can open Java JAR files with any Zip utility such as 7-Zip or WinRAR.

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
  • 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 -jar to -cp.

When the main-class is not specified in the manifest file, you must use the classpath flag (-cp), not the -jar flag.

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, just make sure you have Java installed, your environment is properly configured, and the JAR file has the application’s entry-point listed in the manifest file. With all of these puzzle pieces in place, your Java JAR file will run without issues.

SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close