简体   繁体   中英

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 (ie those not contained in the nested 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.

您可以使用maven shade插件生成一个包含所有内容的jar。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM