我有一个使用多个外部库的Java程序。该程序在 Eclipse IDE 中编译和运行,没有任何问题。但是,当使用 ant build.xml 时,编译工作正常,但在运行时会引发以下异常。
run:
[java] Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/sun/jna/Library
[java] at java.lang.ClassLoader.defineClass1(Native Method)
[java] at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
[java] at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
[java] at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
[java] at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
[java] at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] at org.freedesktop.gstreamer.Gst.getVersion(Unknown Source)
[java] at org.freedesktop.gstreamer.Gst.init(Unknown Source)
[java] at org.freedesktop.gstreamer.Gst.init(Unknown Source)
[java] at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
[java] at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
[java] at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
[java] at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
[java] at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
[java] at java.awt.Component.processMouseEvent(Component.java:6539)
[java] at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
[java] at java.awt.Component.processEvent(Component.java:6304)
[java] at java.awt.Container.processEvent(Container.java:2239)
[java] at java.awt.Component.dispatchEventImpl(Component.java:4889)
[java] at java.awt.Container.dispatchEventImpl(Container.java:2297)
[java] at java.awt.Component.dispatchEvent(Component.java:4711)
[java] at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
[java] at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
[java] at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
[java] at java.awt.Container.dispatchEventImpl(Container.java:2283)
[java] at java.awt.Window.dispatchEventImpl(Window.java:2746)
[java] at java.awt.Component.dispatchEvent(Component.java:4711)
[java] at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
[java] at java.awt.EventQueue.access$500(EventQueue.java:97)
[java] at java.awt.EventQueue$3.run(EventQueue.java:709)
[java] at java.awt.EventQueue$3.run(EventQueue.java:703)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
[java] at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
[java] at java.awt.EventQueue$4.run(EventQueue.java:733)
[java] at java.awt.EventQueue$4.run(EventQueue.java:731)
[java] at java.security.AccessController.doPrivileged(Native Method)
[java] at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
[java] at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
[java] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
[java] at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
[java] at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
[java] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
[java] at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
[java] Caused by: java.lang.ClassNotFoundException: com.sun.jna.Library
[java] at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[java] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] ... 52 more
下面给出了我正在使用的构建脚本。
<property name="src.dir" value="src/org"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="lib.dir" value="Libs" />
<property name="main-class" value="org.ai.receiver.DashboardLauncher"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}/ai/receiver:${src.dir}/freedesktop/gstreamer" destdir="${classes.dir}" includeantruntime="false">
<classpath>
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</classpath>
</javac>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true">
<classpath>
<pathelement location="Libs/jna.jar:Libs/jna-platform.jar:Libs/swt.jar"/>
<fileset dir="${lib.dir}">
<include name="**/*.jar" />
</fileset>
</classpath>
</java>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
所需的外部库 "jna.jar"、"jna-platform.jar" 和 "swt.jar" 文件位于 "Lib" 目录中。
在我看来,这些外部库在运行时没有正确加载,但它们在编译时正确链接。此 github 问题跟踪器中讨论了类似的问题,但讨论没有提供适当的解决方案。
你能帮我解决这个问题吗?
谢谢。
此问题已通过更改.jar的目标得到解决,如下所示:
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
<zipgroupfileset dir="Libs" includes="*.jar" />
</jar>
</target>