我在 eclipse 中创建了一个程序,并将其导出到 JAR,但是当我尝试运行它时,出现以下错误:
Error: Could not find or load main class
我不明白为什么它找不到它,因为我已经说明了主类在哪里。我的 list 文件如下所示:
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 上使用 java,并使用以下版本的 JDK:
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
最佳答案
问题在于 list 中 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
条目中未指定该类。
关于java - 错误: Could not find or load main class on Mac OSX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17077383/