我使用 ant 创建了一个可运行的jar,但它没有运行。当我运行jar时,我收到了以下错误
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/teamdev/jxbrowser/events/NavigationListener
at com.MainClass$2.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: java.lang.ClassNotFoundException: com.teamdev.jxbrowser.events.NavigationListener
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)
... 15 more
但是当我使用eclipse创建jar时,jar工作得很好。我用ant创建的jar的清单如下
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_37-b06-434-11M3909 (Apple Inc.)
Main-Class: com.MainClass
Class-Path: ./ commons-cli.jar commons-codec-1.2.jar commons-httpclien
t-3.0.1.jar commons-logging-1.1.1.jar ffmpeg-java.jar fmj.jar jdom.ja
r logback-classic.jar logback-core.jar lti-civil-no_s_w_t.jar mail.ja
r mp3spi1.9.4.jar slf4j-api.jar tritonus_share.jar vorbisspi1.0.2.jar
xuggle-xuggler.jar comfyj-2.9.jar engine-gecko.jar engine-ie.jar eng
ine-webkit.jar jniwrap-3.8.4 jxbrowser-3.3.jar jxbrowserdemo.jar log4
j-1.2.15.jar MozillaInterfaces.jar runtime.jar slf4j-api-1.5.8.jar sl
f4j-log4j12-1.5.8.jar winpack-3.8.3.jar xulrunner-mac.jar
我用eclipse创建的jar的清单如下所示
Manifest-Version: 1.0
Rsrc-Class-Path: ./ jniwrap-3.8.4.jar lti-civil-no_s_w_t.jar commons-h
ttpclient-3.0.1.jar slf4j-log4j12-1.5.8.jar vorbisspi1.0.2.jar slf4j-
api.jar ffmpeg-java.jar fmj.jar commons-codec-1.2.jar engine-ie.jar x
ulrunner-mac.jar commons-logging-1.1.1.jar winpack-3.8.3.jar mp3spi1.
9.4.jar tritonus_share.jar commons-cli.jar jdom.jar MozillaInterfaces
.jar comfyj-2.9.jar jxbrowser-3.3.jar runtime.jar logback-classic.jar
engine-gecko.jar mail.jar engine-webkit.jar log4j-1.2.15.jar jxbrows
erdemo.jar slf4j-api-1.5.8.jar logback-core.jar xuggle-xuggler.jar
Class-Path: .
Rsrc-Main-Class: com.MainClass
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
两者都是相同的,并且所有依赖关系也在创建的jar中,但是仍然使用ant创建的jar不起作用 我知道你会建议我用eclipse创建jar但是我想混淆我的代码,这就是为什么我用ant来创建runnable jar。
答案 0 :(得分:1)
使用Ant生成的Manifest,它正在查找主jar文件之外的库jar文件,Eclipse使用特殊的Main-Class和类加载器来获取捆绑的依赖项。
您可能希望使用OneJar ant task(或类似的东西)来实现与Eclipse相似的功能。
答案 1 :(得分:1)
我猜你用Ant生成的jar文件没有jar-in-jar-loader
,这就是为什么它无法在嵌入式jar中找到类。
使用Eclipse生成JAR时,可以保存Ant脚本,然后将jar-in-jar-loader.zip
文件添加到项目中。然后使用生成的Ant文件来创建JAR。这种方法适合我。
您的Ant脚本应如下所示:
<jar destfile="C:\Users\\workspace\Your.jar">
<manifest>
<attribute name="Main-Class" value="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"/>
<attribute name="Rsrc-Main-Class" value="org.mypackage.MainClass"/>
<attribute name="Class-Path" value="."/>
<attribute name="Rsrc-Class-Path" value="./ libA.jar "/>
</manifest>
<zipfileset src="jar-in-jar-loader.zip"/>
<fileset dir="${ProjectPath}/bin"/>
<zipfileset dir="${ProjectPath}\lib" includes="libA.jar"/>
</jar>
首先zipfileset
将包含jar-in-jar-loader.zip
fileset
会添加所有课程
第二个zipfileset
会将libA.jar添加为嵌入式jar,Rsrc-Class-Path