247

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

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846

11 Answers11

233

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
Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
  • 1
    if Test class refers another class via jar file, in that case what modifications to be done? – Anand Sep 12 '12 at 12:40
  • @Anand then you need to include a Class-Path line in your manifest and reference the other jar(s). http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html – rob May 13 '13 at 21:46
  • Please Help me on this question: http://stackoverflow.com/questions/24740803/could-not-find-or-load-main-class-on-jar-executing – Sajad Jul 15 '14 at 22:17
  • I think having Manifest-version: 1.0 is not necessary because the contents of the given manifest file get appended to the default manifest file that already has the version http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html – Georgii Oleinikov Oct 07 '14 at 22:21
  • you need put in the manifest "Main-Class: Test" only. Also an important here is to pass to new line after this string and to save. Otherwise it will create default manifest inside the jar without declaring main class. – CodeToLife Jan 26 '20 at 18:31
  • 3
    This is just end-to-end sample. Perfect simple crisp. I was looking for something like this for hours. Not sure why whole Internet is full with all junk without such an simple example, when searched for How to create "Hello world" java jar. Many many thanks. – Atul Jan 27 '20 at 07:05
65
java -classpath Predit.jar your.package.name.MainClass
Lynch
  • 8,644
  • 2
  • 22
  • 34
  • If this jar uses other jars, shouldn't the classpath be like "/path/to/jars/*" ? – Maxim Chetrusca Apr 02 '15 at 06:13
  • 6
    @Max Chetrusca Yes but the separator is `:` using `*` will not work because your shell will expand it like this: `"/path/to/jars/a.jar" "/path/to/jars/b.jar"` but what you really want is: `"/path/to/jars/a.jar:/path/to/jars/b.jar"`. – Lynch Apr 03 '15 at 01:57
48

Before run the jar check Main-Class: classname is available or not in MANIFEST.MF file. MANIFEST.MF is present in jar.

java -jar filename.jar
Gavriel Cohen
  • 3,920
  • 32
  • 37
KARTHIKEYAN.A
  • 14,686
  • 4
  • 102
  • 112
20

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.

Florian Fankhauser
  • 3,553
  • 2
  • 24
  • 30
  • 1
    Just of of curiosity, is the 'Meta-inf' subfolder name case sensitive? I have traditionally seen it spelled 'META-INF' – Adam Paynter Aug 06 '09 at 10:58
  • You're right. The spec says "META-INF" and does not say anything about case insensitivity (http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#The%20META-INF%20directory) – Florian Fankhauser Aug 07 '09 at 06:29
18

A very simple approach to create .class, .jar file.

Executing the jar file. No need to worry too much about manifest file. Make it simple and elgant.

Java sample Hello World Program

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

Compiling the class file

javac HelloWorld.java

Creating the jar file

jar cvfe HelloWorld.jar HelloWorld HelloWorld.class

or

jar cvfe HelloWorld.jar HelloWorld *.class

Running the jar file

 java -jar HelloWorld.jar

Or

java -cp HelloWorld.jar HelloWorld
nagendra547
  • 4,484
  • 1
  • 26
  • 41
  • 1
    Yes, the `jar`'s option `e` lets you specify the `Main-Class` (*entrypoint*) and also creates a corresponding manifest file for you. See [the official documentation for jar](https://docs.oracle.com/javase/8/docs/technotes/tools/windows/jar.html). – Petr Bodnár Nov 12 '19 at 19:19
11

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.

Egil
  • 141
  • 4
  • 2
    `-classpath` comes right after `java` and get rid of `-jar` (i.e. this answer is incorrect) – Pete Aug 19 '18 at 01:04
4

Java

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

manifest.mf

Manifest-version: 1.0
Main-Class: Hello

On command Line:

$ jar cfm HelloMss.jar  manifest.mf Hello.class 
$ java -jar HelloMss.jar

Output:

Hello Shahid
Karthikeyan
  • 7,700
  • 10
  • 46
  • 66
Mohammad Shahid Siddiqui
  • 3,374
  • 2
  • 23
  • 12
4

Eclipse Runnable JAR File

Create a Java Project – RunnableJAR

  • If any jar files are used then add them to project build path.
  • Select the class having main() while creating Runnable Jar file. enter image description here

Main Class

public class RunnableMainClass {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("Name : "+args[0]);
        System.out.println(" ID  : "+args[1]);
    }
}

Run Jar file using java program (cmd) by supplying arguments and get the output and display in eclipse console.

public class RunJar { 
    static StringBuilder sb = new StringBuilder();
    public static void main(String[] args) throws IOException {
        String jarfile = "D:\\JarLocation\\myRunnable.jar";
        String name = "Yash";
        String id = "777";

        try { // jarname arguments has to be saperated by spaces
            Process process = Runtime.getRuntime().exec("cmd.exe start /C java -jar "+jarfile+" "+name+" "+id);
                    //.exec("cmd.exe /C start dir java -jar "+jarfile+" "+name+" "+id+" dir");
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream ()));
            String line = null;
            while ((line = br.readLine()) != null){
                sb.append(line).append("\n");
            }
            System.out.println("Console OUTPUT : \n"+sb.toString());
            process.destroy();
        }catch (Exception e){
            System.err.println(e.getMessage());
        }
   }
}

In Eclipse to find Short cuts:

Help ► Help Contents ► Java development user guide ► References ► Menus and Actions

Yash
  • 8,518
  • 2
  • 64
  • 71
4

I have this folder structure:

D:\JavaProjects\OlivePressApp\com\lynda\olivepress\Main.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\press\OlivePress.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\olives\Kalamata.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\olives\Ligurian.class D:\JavaProjects\OlivePressApp\com\lynda\olivepress\olives\Olive.class

Main.class is in package com.lynda.olivepress

There are two other packages:

com.lynda.olivepress.press

com.lynda.olivepress.olive

1) Create a file named "Manifest.txt" with Two Lines, First with Main-Class and a Second Empty Line.

Main-Class: com.lynda.olivepress.Main

D:\JavaProjects\OlivePressApp\ Manifest.txt

2) Create JAR with Manifest and Main-Class Entry Point

D:\JavaProjects\OlivePressApp>jar cfm OlivePressApp.jar Manifest.txt com/lynda/olivepress/Main.class com/lynda/olivepress/*

3) Run JAR

java -jar OlivePressApp.jar

Note: com/lynda/olivepress/* means including the other two packages mentioned above, before point 1)

Gabriel D
  • 41
  • 1
3

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

fortran
  • 71,170
  • 24
  • 131
  • 172
-1

To run jar, first u have to create

executable jar

then

java -jar xyz.jar

command will work

Hemang
  • 26,372
  • 18
  • 120
  • 180
Deepa
  • 7