开发者

Getting Java Applet to find jar libraries embedded in single jar file

I'm having some trouble getting my Java Applet, which uses embedded jar libraries to work in a web page. Specifically I get a NoClassDefFoundError for classes that are contained in libraries packaged inside jar files which are themselves contained in the single jar file of my Applet. The regular classes of the latter (i.e. those not contained in the nest开发者_开发百科ed jar files) are located by the Applet launcher without any problem.

Here's my HTML code:

<applet code="Applet.class" archive="myApplet.jar" width="600" height="600" title="MyApplet">

Say, inside the MyApplet.jar there are two further jar files: library1.jar and library2.jar at the root level. How do I make them accessible so that the launcher can find them? I have tried adding library1.jar and library2.jar to the archive attribute but that didn't work.


You could unjar the files from your dependency jars, and include them in your jar. Say you have a bin, src, and libs dir.

cd bin
find ../libs -type f -name \*.jar -exec jar xfv {} \;
rm -rf bin/META-INF
cd ..
jar cfv yourjar.jar -C bin/ .


Java does not support jar files inside other jar files directly.

You will need a custom classloader which knows how to work with them. The one-jar project provides such a classloader and is scriptable with ant. If you use Eclipse, the Export -> Runnable jar has an option to create a single jar with the one-jar classloader directly.

Note that memory usage will be higher as the classloader need to cache more information in memory.


You can use the maven shade plugin to generate one jar containing everything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜