Runnable jar file generated by Eclipse wont execute?

巧了我就是萌 提交于 2019-12-12 07:06:54

问题


I generated a very simple runnable jar file using Eclipse's "Export-->Java-->Runnable Jar File" function. My HelloWorld class looks like this:

import javax.swing.JFrame;
public class HWorld extends JFrame {
  public static void main(String[] args) {
    new HWorld();
  }
  public HWorld() {
    this.setSize(200, 100);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Hello World!");
    this.setVisible(true);
  }
}

Now, after generating the .jar file , it runs fine from the command line using the command "java -jar HWorld.jar"

But, when I try to execute the jar on its own (which supposedly should work) I get the following error and I don't know why:

E:\Eclipse\workspace>HWorld.jar
Exception in thread "main" java.lang.NoClassDefFoundError: E:\Eclipse\workspace\HWorld/jar
Caused by: java.lang.ClassNotFoundException: E:\Eclipse\workspace\HWorld.jar
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: E:\Eclipse\workspace\HWorld.jar.  Program will exit.

My manifest looks like this:

Manifest-Version: 1.0
Rsrc-Class-Path: ./
Class-Path: .
Rsrc-Main-Class: HWorld
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

The only thing that looks really fishy to me is this (since a .jar is not a .class):

Could not find the main class: E:\Eclipse\workspace\HWorld.jar

Looking for ideas or thoughts or even an answer! I tried to give as much info as possible in hope of a quality answer. This thread implies that it should work but doesn't answer my question: http://forums.oracle.com/forums/thread.jspa?threadID=2152988 . Can anyone else try it in their Eclipse?


回答1:


Jars are never 'executable' in this sense. What this jar is good for is:

java -jar YOURJAR.jar

update the backtrace you supplied is bizarre. It appears that Windows decided to go ahead and launch your jar with some version of Java, but pass it a pathname in the place of a class name. I don't know what the story is with that.




回答2:


Uninstalling all older java versions on my machine fixed the issue for me.

At my end I was able to run the .jar file with the command line, but not with the default double-click option. Afterwards the latter was working again




回答3:


In my case, by mistake I had not declared the class containing public static void main() as a public class. After declaring the class as public was able to resolve this issue with the next export as runnable jar via eclipse...

Hope this helps...



来源:https://stackoverflow.com/questions/4881136/runnable-jar-file-generated-by-eclipse-wont-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!