How to run a JAR file
Andrew Mclaughlin
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.jarIt raised "Failed to load main class" exceptions. So I extracted this JAR file:
jar -xf Predit.jarand I ran the class file:
java PreditIt worked well. I do not know why the JAR file did not work. Please tell me the steps to run the JAR file
11 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: TestNote 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.jarOutput:
Hello world 6 java -classpath Predit.jar your.package.name.MainClass 2 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 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.MainClassChange '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.
2A 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.javaCreating the jar file
jar cvfe HelloWorld.jar HelloWorld HelloWorld.classor
jar cvfe HelloWorld.jar HelloWorld *.classRunning the jar file
java -jar HelloWorld.jarOr
java -cp HelloWorld.jar HelloWorld 1 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.TestThis sets the which main-class to run in the jar file.
1Java
class Hello{ public static void main(String [] args){ System.out.println("Hello Shahid"); }
}manifest.mf
Manifest-version: 1.0
Main-Class: HelloOn command Line:
$ jar cfm HelloMss.jar manifest.mf Hello.class
$ java -jar HelloMss.jarOutput:
Hello Shahid 0 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.
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
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.MainD:\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)
If you don't want to deal with those details, you can also use the export jar assistants from Eclipse or NetBeans.
1To run jar, first u have to create
executable jar
then
java -jar xyz.jar
command will work
0