The weird thing is, if I copy all of these files into the SAME folder as the JAR file, then it works. I would like to just have a single JAR, however and be able to execute it by itself.
You can't include jar files inside other jar files.
Your manifest file should have a Class-Path entry listing each of the jar files you need to reference, see the Sun Tutorial.
The jar file needs to contain .class files, not .java files. And more importantly, jar files that are inside of other jar files can't be used for the classpath.
You can't include jar files inside other jar files.
Your manifest file should have a Class-Path entry listing each of the jar files you need to reference, see the Sun Tutorial.
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. I now deleted the files and folders that I don't need to execute the jar and my folder now contains the following:
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.
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.
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.
Post by:autobot
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, the permaculture playing cards