• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Set library classpath inside runnable JAR file

 
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
john price
Ranch Hand
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 495
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was just looking at something...Would URLClassLoader do me any good?

Thanks,
John Price
 
Destroy anything that stands in your way. Except this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic