This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Executing testapp.jar now works! However, if I remove the other jar files from the folder and execute it, I get a missing class error, presumably because it requires the jar files.
One last question. I think I understand what is happening, however, why is it the other JAR files (mq.jar etc) are able to be packaged within testapp.jar if they are not reachable? It would be nice to have just one single executable jar file.
Thanks for the assistance.
Joined: Mar 22, 2005
A jar file is just a zip file with some special properties - as such it can contain any kind of file. There may be other reasons (besides wanting to load classes contained in them) why one might want to keep nested jar files.
Note that it's just the standard URLClassLoader (which the JVM uses by default) that can't look for jar files inside of jar files. Nothing prevents you from writing a classloader that *can* load classes from nested jar files. You can switch that for the standard ClassLoader by using the "java.system.class.loader" property. See the javadocs of the ClassLoader class for more detail.
Joined: Aug 07, 2007
Christopher Frankland wrote:Interesting. I compiled the JAR file using the following:
jar cfvm testapp.jar META-INF\MANIFEST.MF *.jar ar/mq/*.class
It generates the testapp.jar file places the jar and class files into that jar.
Well it would because you've told it to add all jar and all class files.
When I said you can't include jar files in other jar files I was being very simplistic, ie I meant your jar isn't working because the other jars it needs are packaged in it - sorry I didn't explain it in clearer terms. As Ulf has already explained you can put anything into a jar, the problem is the Java classloader won't access jars that are within jars.
If you really want a single executable then there are tools that will package everything up into a single .exe file, but this means they will only run on a Windows platform. I'm not sure if they are also available for other platforms and either way I don't advocate their use, I'm just aware they are available.