Pojo
Pojo

Reputation: 1237

Could not find the main class, program will exit

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.

Upvotes: 26

Views: 131470

Answers (8)

Krish
Krish

Reputation: 21

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.

Upvotes: 1

mgr
mgr

Reputation: 651

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

Upvotes: 1

Murtuza
Murtuza

Reputation: 382

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.

Upvotes: 1

Amila Jayawardhana
Amila Jayawardhana

Reputation: 168

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.

Upvotes: 2

Rahul
Rahul

Reputation: 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

Upvotes: -1

KV Prajapati
KV Prajapati

Reputation: 94645

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

Upvotes: 3

Pojo
Pojo

Reputation: 1237

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.

Upvotes: 42

RHT
RHT

Reputation: 5054

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.

Upvotes: 0

Related Questions