Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

JAR's inside a JAR + custom ClassLoader

Thread view: 
Karsten Wutzke - 25 Jun 2007 17:31 GMT
Hi all!

I want to bundle my app in one JAR file. In the lib sub dir of the
JAR, there should be all JAR's that the application depends on, here
bcel-5.2.jar (plus several others).

Since adding the JAR's to the manifest's Class-Path entry didn't work,
I tried my luck with a URLClassLoader. The code itself is too easy. I
have no real idea of how to tell the runtime when and how to use that
class loader. I read the class loader docs which explained the
delegation model.

So I did:

URL urlLib1 = RuntimeConfig.class.getResource("/lib/bcel-5.2.jar");

URLClassLoader cllLibs = new URLClassLoader(new URL[]{urlLib1});

I expected the class loader to become a child of the bootstrap class
loader.

The problem is: I don't really know where to put the above code.
Currently it is in my "config" class. However, the class loader
doesn't seem to be put into the delegation hierarchy, otherwise the
classloader would find the classes from the bcel JAR. I checked the
URL and JAR location inside my JAR, they seem to be correct.

As soon as one of the classes get referenced, here
org.apache.bcel.generic.Type, the (main) thread stops with a
NoClassDefFoundException.

Does anyone know what I am doing wrong? How do I make it work?

TIA
Karsten
Christian - 25 Jun 2007 17:59 GMT
Karsten Wutzke schrieb:
> Hi all!
>
> Does anyone know what I am doing wrong? How do I make it work?
>
> TIA
> Karsten

Have you heard of fatjar till now?

http://fjep.sourceforge.net/

it seems to be pretty close on what you are doing..

christian
Tom Hawtin - 25 Jun 2007 18:03 GMT
> I want to bundle my app in one JAR file. In the lib sub dir of the
> JAR, there should be all JAR's that the application depends on, here
> bcel-5.2.jar (plus several others).

You are probably better off unjarring the jars and jarring the lot together.

> Since adding the JAR's to the manifest's Class-Path entry didn't work,
> I tried my luck with a URLClassLoader. The code itself is too easy. I
[quoted text clipped - 10 lines]
> I expected the class loader to become a child of the bootstrap class
> loader.

If you use the constructor that doesn't specify the parent, then you get
the system class loader as parent. This is what the API docs mean by
"the default delegation parent ClassLoader". Why they use such an obtuse
phrase, I don't know. If you want the bootstrap class loader to be the
direct parent, add ", null".

> The problem is: I don't really know where to put the above code.
> Currently it is in my "config" class. However, the class loader
> doesn't seem to be put into the delegation hierarchy, otherwise the
> classloader would find the classes from the bcel JAR. I checked the
> URL and JAR location inside my JAR, they seem to be correct.

The delegation happens the other way around. A class loader delegates to
its parent. So your class loader delegates to the system (and hence
boot) class loader, not the other way around. You need to specify your
class loader to load a class from it, for instance using
Class.forName(String,boolean,ClassLoader).

If you want to link your own code to that, then you either need another
class loader that delegates to your "cllLibs" class loader, or make one
class loader to load it all (other than your initial code to create the
class loader and kick off the app).

Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.