Reputation: 857
I have created a folder named "Thank" in c drive and in that folder, I have 2 java files namely T1 and myApp.
class T1 {
void display() {
System.out.println("Hey I am working");
}
}
and
class myApp {
public static void main(String args[]) {
T1 t=new T1();
t.display();
}
}
Now I created a jar file of this folder by:
c:\>jar cf myApp.jar Thank
This creates a Jar file named myApp.
I even wrote Main-Class: myApp in the manifest.mf file
.
When I try to run this by:
c:\>java -jar myApp.jar
I get an error -
An unexpected error occurred while trying to open file myApp.jar
Please tell me how to run jar file so that I get an output:
Hey I am working
Upvotes: 0
Views: 2350
Reputation: 4555
You should use jar cfm myApp.jar manifest.txt *.class
to create the jar, so that the manifest file gets located correctly in the jar.
The right location of the manifest is META-INF/MANIFEST.MF
.
I have made your code work, basically by taking the files you had prepared; adding a package declaration to the java files and the manifest, and upper-casing the MyApp class from myApp. The files are arranged in this folder structure:
tbsmac:17162802-executing-java-file-through-cmd tbsalling$ ls -lR
total 0
drwxr-xr-x 3 tbsalling staff 102 19 Jun 18:48 META-INF
drwxr-xr-x 4 tbsalling staff 136 19 Jun 18:57 thank
./META-INF:
total 8
-rw-r--r-- 1 tbsalling staff 46 19 Jun 18:49 MANIFEST.MF
./thank:
total 16
-rw-r--r-- 1 tbsalling staff 124 19 Jun 18:49 MyApp.java
-rw-r--r-- 1 tbsalling staff 98 19 Jun 18:48 T1.java
The contents of the three files are:
MyApp.java:
package thank;
class MyApp {
public static void main(String args[]) {
T1 t=new T1();
t.display();
}
}
T1.java:
package thank;
class T1 {
void display() {
System.out.println("Hey I am working");
}
}
MANIFEST.MF:
Main-Class: thank.MyApp
Manifest-Version: 1.0
Then I run this series of commands:
tbsmac:17162802-executing-java-file-through-cmd tbsalling$ javac thank/T1.java thank/MyApp.java
tbsmac:17162802-executing-java-file-through-cmd tbsalling$ jar cfm myApp.jar META-INF/MANIFEST.MF thank/*.class
tbsmac:17162802-executing-java-file-through-cmd tbsalling$ java -jar myApp.jar
Hey I am working
^^^ and it works ;-)
Upvotes: 2
Reputation: 68715
Couple of things:
Make sure you add the complete class name along with package names in manifest file.
Also make sure to add a newline at the end of your jar manifest file. If not do so.
Upvotes: 0
Reputation: 2040
try by typing: java.exe -jar "(full path to jar)"
example: java.exe -jar "C:\TestFolder\Project Java\myApp.jar"
Upvotes: 0