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
Rohit Suthar
  • 3,528
  • 1
  • 42
  • 48
Neha Gupta
  • 847
  • 1
  • 8
  • 15
  • 1
    have a look.. http://stackoverflow.com/questions/7368349/nullpointerexception-when-trying-to-run-jar-file – Gaurav Manral Jun 18 '13 at 07:23
  • 1
    What does your complete manifest file look like and where is it stored? – tbsalling Jun 18 '13 at 07:26
  • possible duplicate of [Running Jar file in Windows](http://stackoverflow.com/questions/394616/running-jar-file-in-windows) – Jayan Jun 18 '13 at 07: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) – Neha Gupta Jun 18 '13 at 16:39
  • I have updated my answer below, showing how to make it work. – tbsalling Jun 19 '13 at 17:04

3 Answers3

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

tbsalling
  • 4,477
  • 4
  • 30
  • 51
0

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

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

The Badak
  • 2,010
  • 2
  • 16
  • 28
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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136