26

I made an executable jar with the command prompt in Windows 7 using the

jar cvfm MyJarName.jar manifest.txt *.class

which created the .jar file. But when I attempt to open it, I get a pop-up window that says

Could not find the main class: <ClassName>. Program will exit.

Yet, when I use

java -jar jarName.jar

in the command prompt, it works fine. What's the deal? I want to be able to just double-click it.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Pojo
  • 1,229
  • 4
  • 16
  • 26
  • Show us the content of manifest file – KV Prajapati Oct 01 '11 at 02:42
  • I can't tell from what little you've explained, but in general if your main class is "com.Main", then Main.class needs to exist inside of a "com" folder within the jar. – Chris Eberle Oct 01 '11 at 02:44
  • possible duplicate of [Why it's failed to load main-class manifest attribute from jar-file?](http://stackoverflow.com/questions/2591516/why-its-failed-to-load-main-class-manifest-attribute-from-jar-file) – KV Prajapati Oct 01 '11 at 02:50
  • Well, my manifest file just contains the line "Main-Class: NatTimer" because my class's name is NatTimer... When I extracted the one out of the jar, it had 2 extra lines above it; "Manifest-Version: 1.0" and "Created-By: 1.7.0 (Oracle Corporation)" But that probably isn't the problem, right? Now, when I made the jar, I didn't have any package statement in the code, nor was it in any sort of special folder for jarring. ...That's the problem, isn't it? – Pojo Oct 01 '11 at 13:42

8 Answers8

42

Ha, I found what the problem was. I made my program using jdk1.7, but I had jre6 installed. I went and upgraded to jre7, and now it works fine :)

The

java -jar jarname.jar

line was working in the command prompt because my java path was set to the jdk folder.

Pojo
  • 1,229
  • 4
  • 16
  • 26
  • 2
    I had the same problem, but i had already jre7 installed, but since my application was **32bit** i had to download and install _jre7-i586(x86)_ too. Thanks for the answer! – Gabriel Marcos Jarczun Oct 29 '12 at 14:01
3

If you are using JDK 1.6 or higher then you can override the manifest attribute via e flag of Jar tool. (Read - Setting an Entry Point with the JAR Tool):

Example:

package pack;

public class Test
{
  public static void main(String []args)  
   {
     System.out.println("Hello World");
    }
}

Compile and run Jar tool,

c:\>jar cfe app.jar pack.Test pack/Test.class

Invoke app

c:>java -jar app.jar
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
2

The Manifest 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.

j0k
  • 22,600
  • 28
  • 79
  • 90
Amila
  • 168
  • 1
  • 11
1

I was facing the same problem. What I did is I right clicked the project->properties and from "Select/Binary Format" combo box, I selected JDK 6. Then I did clean and built and now when I click the Jar, It works just fine.

Murtuza
  • 308
  • 2
  • 14
1

if you build the source files with lower version of Java (example Java1.5) and trying to run that program/application with higher version of Java (example java 1.6) you will get this problem. for better explanation see this link. click here

mgr
  • 641
  • 1
  • 8
  • 23
1

I got this issue in opening JMeter 4.0. I fixed as below.

I have JRE 7 installed in Program Files (x86) folder and JDK 8 installed in Program files folder. So I just uninstalled JRE7 from machine. I just kept latest version of JDK. It fixed the problem.

Krish
  • 21
  • 3
0

Extract the jar and compare the contents of the manifest inside the jar with your external manifest.txt. It is quite possible that you will locate the problem.

RHT
  • 4,974
  • 3
  • 26
  • 32
-1

Check out doing this way (works on my machine):

let the file be x.java

  1. compile the file javac x.java
  2. jar cfe k.jar x x.class //k.jar is jar file
  3. java -jar k.jar
Rahul
  • 1