This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Set library classpath inside runnable JAR file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Set library classpath inside runnable JAR file" Watch "Set library classpath inside runnable JAR file" New topic
Author

Set library classpath inside runnable JAR file

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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)?

Thanks,
John Price

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)

Works :

Mf.mf :


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)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

You cannot put JAR files inside JAR files - Java does not support that, it will not be able to load classes from the embedded JAR file.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

If I were to take the class files out of the embedded JAR file and stick them in their respective directories would it work?

Thanks,
John Price
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
If I were to take the class files out of the embedded JAR file and stick them in their respective directories would it work?

Please try and lets us know...

but why don't you just give the other jar files in classpath in the new jar's manifest file you create like below?

Main-Class: JMSThreadsMasterController
Class-Path: activation-1.1.jar
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

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.

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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!

John Price
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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!)
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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,
John Price
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

No, that is not what we are saying.

If we have this structure:



Then the manifest in myapplication.jar would be:


Main-Class: JMSThreadsMasterController
Class-Path: activation-1.1.jar mydependent.jar




If the structure is:


then we get the corresponding manifest:

Main-Class: JMSThreadsMasterController
Class-Path: lib/activation-1.1.jar lib/mydependent.jar


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:"



See
http://download.oracle.com/javase/1.4.2/docs/guide/jar/jarGuide.html
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

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?

Thanks,
John Price

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.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Was just looking at something...Would URLClassLoader do me any good?

Thanks,
John Price
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set library classpath inside runnable JAR file