eclipse 将应用程序导出到 Jar 后,为什么会出现“发生 Java 异常”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13078505/
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
Why do I get "A Java Exception has occurred" after I export my application to Jar?
提问by Arturas M
Well, I exported my application into JAR with eclipse, but now whenever I try running it I get the: "A Java Exception has occurred". When I try to run it through command line it says:
好吧,我使用 Eclipse 将我的应用程序导出到 JAR 中,但是现在每当我尝试运行它时,我都会得到:“发生了 Java 异常”。当我尝试通过命令行运行它时,它说:
C:\Users\Arturas>java -jar D:\Dropbox\EclipseWorkspace\Exports\Dantracio_skaiciu
oklis3.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widge
ts/Composite
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Composite
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
C:\Users\Arturas>
Any solutions to this? I just hate java for this... It never works on desktops when you want it to... sorry I'm really stressed out from this, I'm overreacting...
有什么解决办法吗?我只是为此讨厌 Java...当你想要它时,它永远不会在桌面上工作......对不起,我真的很紧张,我反应过度......
Any ideas what could cause this? I doesn't work on other computers as well. Right now it's using Java 1.7. And the application runs perfectly on Eclipse.
任何想法可能导致这种情况?我在其他电脑上也不工作。现在它使用的是 Java 1.7。并且该应用程序在 Eclipse 上完美运行。
回答by digitaljoel
You need to get your dependencies. Looks like you are depending on swt, which is not part of the core java language. You'll need to get all the third party jars that you depend on and include them in your classpath. It runs perfectly on eclipse because eclipse takes care of setting up the classpath for you.
您需要获取依赖项。看起来您依赖于 swt,它不是核心 Java 语言的一部分。您需要获取您依赖的所有第三方 jar 并将它们包含在您的类路径中。它在 eclipse 上完美运行,因为 eclipse 负责为您设置类路径。
You can set the classpath in your manifest file as described in the documentation, or you could include a script to start your application that will use the -cp
command when running the jar, or you can use something that will package everything into one monster jar like the solutions to this question.
您可以按照文档中的描述在清单文件中设置类路径,或者您可以包含一个脚本来启动您的应用程序,该脚本将-cp
在运行 jar 时使用该命令,或者您可以使用将所有内容打包到一个怪物 jar 中的东西,例如解决这个问题。