错误:无法在Mac OSX上找到或加载主类

时间:2013-06-13 00:06:51

标签: java jar compiler-errors executable-jar

我已经在eclipse中创建了一个程序,我已经导出到JAR但是当我尝试运行它时,我收到以下错误:

Error: Could not find or load main class

我不明白为什么它找不到它,因为我已经说明了主要课程的位置。我的Manifest文件如下所示:

Manifest-Version: 1.0
Main-Class: gui.GeoMapItMain
Class-Path: mysql-connector-java-5.1.25-bin.jar
Class-Path: JMapViewer.jar
Class-Path: colt.jar
Class-Path: metadata-extractor-2.6.4.jar
Class-Path: JMapViewer_src.jar
SplashScreen-Image: images/splash.gif

我在带有Mountain Lion的Mac上使用带有以下JDK版本的java:

javac 1.7.0_13
java version "1.7.0_13"

我现在试过这个:

java -jar GeoMapItJ.jar

给出了如下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/openstreetmap/gui/jmapviewer/interfaces/MapMarker
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
    at java.lang.Class.getMethod0(Class.java:2694)
    at java.lang.Class.getMethod(Class.java:1622)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.openstreetmap.gui.jmapviewer.interfaces.MapMarker
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    ... 6 more

1 个答案:

答案 0 :(得分:1)

问题在于清单中Class-Path的语法要求条目以空格分隔,而不是在单独的行上。请参阅JAR File Specification

拥有多个Class-Path键是错误的。 jar工具会在您传递详细标记时发出警告:

$ jar cvfm foo.jar manifest Foo.class 
Jun 12, 2013 9:53:19 PM java.util.jar.Attributes read
WARNING: Duplicate name in Manifest: Class-Path.
Ensure that the manifest does not have duplicate entries, and
that blank lines separate individual sections in both your
manifest and in the META-INF/MANIFEST.MF entry in the jar file.
Jun 12, 2013 9:53:19 PM java.util.jar.Attributes read
WARNING: Duplicate name in Manifest: Class-Path.
Ensure that the manifest does not have duplicate entries, and
that blank lines separate individual sections in both your
manifest and in the META-INF/MANIFEST.MF entry in the jar file.
Jun 12, 2013 9:53:19 PM java.util.jar.Attributes read
...

正在忽略额外的Class-Path条目,这会导致异常

java.lang.ClassNotFoundException: org.openstreetmap.gui.jmapviewer.interfaces.MapMarker

因为未在处理的Class-Path条目中指定类。