1

我的应用程序使用了很多类,分布在许多 jar 中,所以我想根据类加载的顺序创建一个带有有序 jar 的类路径。当所有这些 jar 都在网络/远程文件系统上时,这将减少启动时间。

我开始试验java -verbose:class -classpath ... mainclass并检查加载了什么。获取时添加新罐子ClassNotFoundException

然后我注意到每次都加载了一些类,而我的应用程序没有真正使用它。我只是举了一个例子:一个空的类,一个空的 start 方法。即使加载这个类,也需要很多其他的罐子。

package test;
public class CheckClassLoader {
  static public void main (String args[]) {
  }
}

请注意,这个类包含在一个包含许多其他类的大 jar 中。

这是我的输出。它显示了我编写的一堆类(不是标准的 java,也不是我使用的外部 jar 中的类)已加载。

$ java -verbose:class -Xdiag -jar Neos.jar test.CheckClassLoader \
  | egrep -v 'Loaded (sun|java|javax|org\.xml)\.'
[Opened /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
[Loaded gui.sendto.SendToListener from file:/tmp/Neos.jar]
[Loaded gui.ParentChildFrameIfc from file:/tmp/Neos.jar]
[Loaded gui.ParentChildFrame from file:/tmp/Neos.jar]
[Loaded gui.MainFrame from file:/tmp/Neos.jar]
[Loaded utils.MessageException from file:/tmp/Neos.jar]
[Loaded exchange.xml.XmlImportReader from file:/tmp/Neos.jar]
[Loaded gui.ParentChildFrame$7 from file:/tmp/Neos.jar]
[Loaded engine.TableBean from file:/tmp/Neos.jar]
[Loaded utils.io.net.ConnectionListener from file:/tmp/Neos.jar]
[Loaded utils.image.ByteDocTools from file:/tmp/Neos.jar]
[Loaded gui.utils.FileFilterExtension from file:/tmp/Neos.jar]
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
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.apache.pdfbox.pdmodel.PDDocument
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

完整输出的最后一部分(未用 过滤egrep)是:

[Loaded engine.TableBean from file:/tmp/Neos.jar]
[Loaded java.io.IOException from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
[Loaded utils.io.net.ConnectionListener from file:/tmp/Neos.jar]
[Loaded utils.image.ByteDocTools from file:/tmp/Neos.jar]
[Loaded javax.swing.filechooser.FileFilter from /home/giuseppe/jdk1.7.0_15/jre/lib    /rt.jar]
[Loaded gui.utils.FileFilterExtension from file:/tmp/Neos.jar]
[Loaded java.io.FileNotFoundException from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
[Loaded java.lang.NoSuchMethodException from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
Exception in thread "main" [Loaded java.lang.Throwable$PrintStreamOrWriter from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
[Loaded java.lang.Throwable$WrappedPrintStream from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
[Loaded java.util.IdentityHashMap from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
[Loaded java.util.IdentityHashMap$KeySet from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/PDDocument
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)
[Loaded java.util.Objects from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
Caused by: java.lang.ClassNotFoundException: org.apache.pdfbox.pdmodel.PDDocument
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
[Loaded java.lang.Shutdown from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /home/giuseppe/jdk1.7.0_15/jre/lib/rt.jar]

所以,我的问题是:为什么没有人真正需要 jvm 从 jar 加载类?

4

1 回答 1

3

您的命令行正在运行 Neos.jar 中的主类,而不是您的测试类。为此,请在命令行中替换-jar为。-cp

于 2013-03-01T11:39:52.477 回答