开发者

Could not find the main class, program will exit

开发者 https://www.devze.com 2023-04-10 01:34 出处:网络
I made an executable jar with the command prompt in Windows 7 using the jar cvfm MyJarName.jar manifest.txt *.class

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: <Class开发者_JAVA技巧Name>. 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.


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.


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


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.


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.


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


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.


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号