我的应用程序使用了很多类,遍布很多jar,所以我想根据类加载的顺序创建一个带有序jar的类路径。当所有这些jar都在网络/远程文件系统上时,这会减少startuo时间。
我开始尝试使用java -verbose:class -classpath ... mainclass
并检查已加载的内容。获取ClassNotFoundException
时添加新的jar。
然后我注意到每次都会加载一些类,而我的应用程序并没有真正使用它。我刚刚做了一个例子:一个空类启动方法。即使是这个类的装载,也需要很多其他的罐子。
package test;
public class CheckClassLoader {
static public void main (String args[]) {
}
}
请注意,此类包含在包含许多其他类的大型jar中。
这是我的输出。它表明我加载了一大堆类(不是标准的java,也不是我使用的externa 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加载类?
答案 0 :(得分:3)
您的命令行正在运行Neos.jar中的主类,不是您的测试类。要实现此目的,请在命令行中将-jar
替换为-cp
。