且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我得到“发生Java异常”在我将应用程序导出到Jar之后?

更新时间:2023-11-19 22:56:16

您需要获得依赖关系。看起来你依赖于swt,这不是核心java语言的一部分。您将需要获取所有依赖于您的第三方jar,并将它们包含在您的类路径中。它可以在eclipse上完美运行,因为eclipse负责为您设置类路径。



您可以按照文档,或者您可以包括一个脚本来启动应用程序,将使用 -cp 命令在运行jar时,或者您可以使用将所有内容打包成一个怪物jar的东西,如这个问题


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:

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$1.run(Unknown Source)
        at java.net.URLClassLoader$1.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...

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.

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.

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.