How to run a JAR file

Viewed 793718

I created a JAR file like this:

jar cf Predit.jar *.*

I ran this JAR file by double clicking on it (it didn't work). So I ran it from the DOS prompt like this:

java -jar Predit.jar

It raised "Failed to load main class" exceptions. So I extracted this JAR file:

jar -xf Predit.jar

and I ran the class file:

java Predit

It worked well. I do not know why the JAR file did not work. Please tell me the steps to run the JAR file

12 Answers

You need to specify a Main-Class in the jar file manifest.

Oracle's tutorial contains a complete demonstration, but here's another one from scratch. You need two files:

Test.java:

public class Test
{
    public static void main(String[] args)
    {
        System.out.println("Hello world");
    }
}

manifest.mf:

Manifest-version: 1.0
Main-Class: Test

Note that the text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.

Then run:

javac Test.java
jar cfm test.jar manifest.mf Test.class
java -jar test.jar

Output:

Hello world
java -classpath Predit.jar your.package.name.MainClass

You have to add a manifest to the jar, which tells the java runtime what the main class is. Create a file 'Manifest.mf' with the following content:

Manifest-Version: 1.0
Main-Class: your.programs.MainClass

Change 'your.programs.MainClass' to your actual main class. Now put the file into the Jar-file, in a subfolder named 'META-INF'. You can use any ZIP-utility for that.

If you don`t want to create a manifest just to run the jar file, you can reference the main-class directly from the command line when you run the jar file.

java -jar Predit.jar -classpath your.package.name.Test

This sets the which main-class to run in the jar file.

If you don't want to deal with those details, you can also use the export jar assistants from Eclipse or NetBeans.

  • Follow this answer, if you've got a jar file, and you need to run it
  • See troubleshooting sections for hints to solve most common errors

Introduction

There are several ways to run java application:

  1. java -jar myjar.jar - is the default option to run application
  2. java -cp my-class-path my-main-class or java -classpath my-class-path my-main-class
  3. java --module-path my-module-path --module my-module/my-main-class
  4. Deployment to an enterprise server. It's when you have war or ear file. We'll omit the explanation for this case

In this answer I'll explain, how to run a jar if you have to run it manually, give hints to resolve common problems.

java -jar

Start with the most common option: run the jar file using the -jar. Example:

java -jar myjar.jar

If it fails:

  • with no main manifest attribute, then the jar is not executable:
  • with other error, then see "Troubleshooting" section below

Classpath or module path

If -jar failed, then the jar should be run using classpath or module-path.

Module-path is used, when an application is modular itself. JPMS - Java Platform Module System - is a modern way to develop, distribute and run applications. For details:

  1. Watch excellent Modular Development with JDK 9 by Alex Buckley
  2. See awesome-java-module-system

To run a jar:

  1. Determine if it's modular or not:
    1. Invoke:
      jar --describe-module --file=path-to-jar-file
      
  2. Examine output:
    1. If you see No module descriptor found. in the first line, then proceed with classpath solution below
    2. If you see something similar to:
    org.diligentsnail.consoleconsumer@1.0-SNAPSHOT jar:file:///home/caco3/IdeaProjects/maven-multi-module-project-demo/jars/console-consumer.jar!/module-info.class
    requires java.base mandated
    requires org.diligensnail.hellolibrary
    
    continue with module-path solution below

See also: List modules in jar file

Classpath

Try the following:

java -cp my-jar.jar my-main-class
  • -cp is the same as -classpath
  • my-jar.jar is the jar to run
  • my-main-class is name of the class with static void main(String[]) method

Example:

java -cp jars/console-consumer.jar org.diligentsnail.consoleconsumer.Main

Module-path

Try the following command:

java --module-path my-jar.jar --module my-module-name/my-main-class
  • my-jar.jar is the jar to run
  • my-module-name is name of the module where my-main-class belongs
    • Usually my-module-name is in the module-info.java file
  • my-main-class - the class with the static void main(String[]) method

If it fails with FindException:

  • Example of message:
    Error occurred during initialization of boot layer
    java.lang.module.FindException: Module javafx.fxml not found, required by org.diligentsnail.javafxconsumer
    
  • Usually, this means the my-jar.jar has a dependency on the other jar. For example, the application uses a third party library. See "Supplying dependencies" below

Troubleshooting

UnsupportedClassVersionError

Update java. See List of Java class file format major version numbers?

NoClassDefFoundError, NoSuchMethodError, NoSuchFieldError

See:

  1. "Supplying dependencies" section
  2. Why am I getting a NoClassDefFoundError in Java?

Supplying dependencies

An Error or Exception is thrown when an application run with missing or out of date dependencies. Common exceptions and errors:

  1. NoClassDefFoundError, NoSuchFieldError, NoSuchMethodError
  2. ClassNotFoundException, NoSuchFieldException, NoSuchMethodException
  3. FindException

To supply dependencies:

  1. Determine the list of dependencies
    1. Usually it's a list of jar or can be a list of directories or both
  2. Join the list with : if you're running Unix, ; - if you're on Windows
  3. Invoke java with -classpath or --module-path

Example

  • Project maven-multimodule-project-demo
  • I'm trying to run console-consumer.jar:
    • Command:
      java -classpath jars/console-consumer.jar org.diligentsnail.consoleconsumer.Main
      
    • jars/console-consumer.jar is the jar I'm trying to run
    • org.diligentsnail.consoleconsumer.Main is the class with main method
  • Error I get:
    Exception in thread "main" java.lang.NoClassDefFoundError: org/diligentsnail/hellolibrary/Hello
      at org.diligentsnail.consoleconsumer.Main.main(Main.java:11)
    Caused by: java.lang.ClassNotFoundException: org.diligentsnail.hellolibrary.Hello
      at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
      at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
      at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
      ... 1 more
    
  • Missing dependency is jars/hello-library.jar
  • Correct command:
    java -classpath jars/console-consumer.jar:jars/hello-library.jar org.diligentsnail.consoleconsumer.Main
    
Related