I'm not sure if this is possible, but I've never been able to do it. I want to be able to have a JAR file (a library) in my runnable JAR file. The JAR file that I am running needs to use the library. How am I able to put the library in my runnable JAR file and use it? How do I set the classpath to be inside my JAR (for the library JAR)?
I do know that I can put the library in the runnable JAR file. I just don't know how to tell the JVM to use it on startup. I have tried the manifest option and have gotten stuck. Please help!
EDIT : (Example)
NOTICE : jl1.0.1.jar is the library needed to play. If I move the jar file to the desktop...
Is there any way to get this to work? Note that I am distributing so the users WILL NOT be downloading and setting the class-path for this...
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
The normal way to do it, like if you use any IDE-package/export function, is to have your main jar in the top directory and any referenced jar under another subdirectory, say /lib, and then reference to the dependent jar-files from the main. As John Jai is suggesting.
Don't "repack" the classes into your own jar-file. That make it hard to update the application and you might have a hard time getting paranoid people like me to use your application. Whenever I see a class that I know belongs to say activation-1.1.jar that is repacked, I immediately ask myself the question "what have they manipulated"? If the activation-1.1.jar is signed or/and there is an sha1/md5 that I can compare to (like the one that can be found at http://mirrors.ibiblio.org/pub/mirrors/maven2/javax/activation/activation/1.1/), then I know that the jar is not manipulated.
I'm not using an IDE (I don't like them). I am just using a syntax editor and a command prompt. Is there an easy way to do this through these venues? Didn't I just say this :
I'm not understanding what you guys are saying different than I am. From what I understand, you guys are saying take the library and stick it under a dir in my JAR file. Then, set the class path to be under the the library.
OR you guys are saying :
Put the library file above the directory in my JAR file.
If either one of these are right, or if they are both wrong, please tell me what you guys are saying and how to fix it. The app that I am working on will be an Applet and will be signed (doubt if this changes anything, but it will have to use the library). Thanks for your input!
Joined: May 31, 2011
Apologies - i meant to say that declaring your other jars in the manifest will be enough. You do not need to add the jar file into the creating jar / unpack the jar files...
1. Set class path in manifest.
2. Place the other jar files in the SAME directory you will place the newly created jar like below (i have not tried the other way though!)
I'm not sure if you are saying this, but this is what I am getting I can do :
1. Create a JAR file based on my current code.
2. Place the library in the same directory.
3. Create a JAR around both of the JAR's, pointing the class-path to the library and the main-class to the JAR.
It needs to be in one contained JAR file. It will be an Applet, so it needs to all be there in one convenient package.
Thanks for your clarification and help,
Then I usually pack and ship the whole thing in a Zip-file or in an installer.
If you pack a jar in an jar, then you can actually from your program ask to get the jar-file as a resource and then programmatically look into that resource with the Zip/Jar-classes. But that is not what you want to do in this case.
Edit: Now I noticed that you are doing an applet.
Then you need to define all the used libraries in the applet-tags archive attribute. Like this:
"The archive tag may specify multiple Jar files. Each Jar file must be separated by "," (comma). Each file is downloaded in turn:"
So from what I understand, I can just include the library in my code base ("URL =" parameter) and archive? I know this is moving to Applet questions... Then, in my Applet program, I can just set the class-path as "library.jar " if the archive contains "library.jar" and "program.jar". Can I set multiple URL's for the code bases to work with?
EDIT : Never mind on the last question (About the multiple URL's). I just ran a test and found that I can specify a folder on the internet instead of a specific file. Thanks for your help.