Eclipse 生成的可运行 jar 文件无法执行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4881136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 19:55:05  来源:igfitidea点击:

Runnable jar file generated by Eclipse wont execute?

eclipsejarexecutable-jar

提问by djangofan

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

我使用 Eclipse 的“Export-->Java-->Runnable Jar File”功能生成了一个非常简单的可运行 jar 文件。我的 HelloWorld 类如下所示:

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"

现在,生成 .jar 文件后,它可以从命令行使用命令“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:

但是,当我尝试自己执行 jar 时(应该可以),我收到以下错误,我不知道为什么:

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.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):

对我来说,唯一看起来很可疑的是(因为 .jar 不是 .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?

寻找想法或想法甚至答案!我试图提供尽可能多的信息,希望得到一个高质量的答案。这个线程暗示它应该工作,但没有回答我的问题:http: //forums.oracle.com/forums/thread.jspa?threadID=2152988。其他人可以在他们的 Eclipse 中尝试吗?

回答by bmargulies

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

从这个意义上说,罐子永远不是“可执行的”。这个罐子的好处是:

java -jar YOURJAR.jar

updatethe 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.

更新您提供的回溯很奇怪。似乎 Windows 决定继续使用某些版本的 Java 启动您的 jar,但将路径名传递给它来代替类名。我不知道那是什么故事。

回答by Rick Hindriks

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

在我的机器上卸载所有旧的 Java 版本为我解决了这个问题。

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

最后,我能够使用命令行运行 .jar 文件,但不能使用默认的双击选项。后来后者又开始工作了

回答by Indira Pai

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

就我而言,我错误地没有将包含 public static void main() 的类声明为公共类。在将类声明为 public 后,能够通过 eclipse 将下一个导出为可运行的 jar 来解决此问题...

Hope this helps...

希望这可以帮助...