0

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 
5
  • 1
    have a look.. stackoverflow.com/questions/7368349/… Commented Jun 18, 2013 at 7:23
  • 1
    What does your complete manifest file look like and where is it stored? Commented Jun 18, 2013 at 7:26
  • possible duplicate of Running Jar file in Windows Commented Jun 18, 2013 at 7:32
  • @tbsalling myApp.jar is stored in c drive. It further has 2 folders namely: META-INF & Thank. META-INF has a MANIFEST.MF file which contains: Main-Class: myApp; Manifest-Version: 1.0 Created-By: 1.7.0_21 (Oracle Corporation) Commented Jun 18, 2013 at 16:39
  • I have updated my answer below, showing how to make it work. Commented Jun 19, 2013 at 17:04

3 Answers 3

2

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.

Update

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 ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

Should I replace "MyApp.class" with "*" ?
0

try by typing: java.exe -jar "(full path to jar)"

example: java.exe -jar "C:\TestFolder\Project Java\myApp.jar"

1 Comment

It again says that an unexpected error occurs while opening c:\myApp.jar
0

Couple of things:

  1. Make sure you add the complete class name along with package names in manifest file.

  2. Also make sure to add a newline at the end of your jar manifest file. If not do so.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.