简体   繁体   English

设置了类路径,但仍收到NoClassDefFoundError

[英]Classpath is set, but still getting NoClassDefFoundError

I'm trying to package a very simple app into a jar to distribute to a user. 我正在尝试将一个非常简单的应用程序打包到一个jar中,以分发给用户。 I'm using Eclipse Indigo with the m2e plugin on Mac OS X 10.6.8. 我在Mac OS X 10.6.8上将Eclipse Indigo与m2e插件一起使用。

I used the Maven build: package goal to package it as a jar. 我使用Maven build:package目标将其打包为jar。 If I double click on the jar to open it, the GUI opens and appears to be working, but when I click the execute button, nothing happens. 如果我双击罐子将其打开,则GUI会打开并似乎正在运行,但是当我单击执行按钮时,什么也不会发生。 (The program works fine if I do Run As... within Eclipse). (如果我在Eclipse中运行方式为...,则该程序可以正常运行)。

If I try to run the jar from the command line, it won't even open the GUI, I get this error: 如果我尝试从命令行运行jar,它甚至都不会打开GUI,我会收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: str-profile-comparison-0/0/1-SNAPSHOT    /jar
Caused by: java.lang.ClassNotFoundException: str-profile-comparison-0.0.1-SNAPSHOT.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)

I extracted the manifest file from the jar, to check the classpath, and it looks like this: 我从jar中提取了清单文件,以检查类路径,它看起来像这样:

Manifest-Version: 1.0
Built-By: username
Build-Jdk: 1.6.0_24
Class-Path: poi-3.7.jar poi-ooxml-3.7.jar poi-examples-3.7.jar xmlbean
s-2.3.0.jar stax-api-1.0.1.jar dom4j-1.6.1.jar xml-apis-1.0.b2.jar po
i-ooxml-schemas-3.7.jar geronimo-stax-api_1.0_spec-1.0.jar poi-scratc
hpad-3.7.jar junit-4.8.2.jar
Created-By: Apache Maven
Main-Class: com.{companyname}.strprofilecomparison.logic.StrComparatorGUI
key: value
url:
mode: development
Archiver-Version: Plexus Archiver

I have several questions about what is happening here: 我对这里发生的事情有几个疑问:

  1. Why does the GUI open when I double-click the jar file, but not when I try to run it at the command line? 为何双击jar文件时会打开GUI,而当我尝试在命令行中运行时却无法打开GUI?

  2. I'm guessing the classpath needs to define the absolute path to the external jars I'm using, how do I set that in the POM file? 我猜想classpath需要定义我正在使用的外部jar的绝对路径,如何在POM文件中设置它?

Thanks! 谢谢!

That manifest says that all your dependencies are sitting in the same dir as your jar. 该清单表明您的所有依赖项都与jar放在同一目录中。 If that's not true, no go. 如果那不是真的,那就不要。

Personally, I'd use the appassembler plugin to make a device that has a shell script to launch it. 就个人而言,我将使用appassembler插件来制作一个具有外壳程序脚本来启动它的设备。 Or the shade plugin to make an uberjar that just incorporates them. 或使用shade插件制作一个将它们合并的uberjar。 Or learn about OSX stubs to make an OSX proper .app application. 或了解有关使OSX成为.app应用程序的OSX存根的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM