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
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
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 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
If you don't want to deal with those details, you can also use the export jar assistants from Eclipse or NetBeans.
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)
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