Is it possible to merge ClassLoaders?

Is it possible to merge ClassLoaders?

Post by Marku » Fri, 25 Nov 2005 19:21:27


I created a URLClassLoader which can load JARs from a given URL.

But the calsses inside the Jars are not be found.

Is there a possibiliy to merge the default ClassLoader with my own one?

Markus
 
 
 

Is it possible to merge ClassLoaders?

Post by Thomas Haw » Fri, 25 Nov 2005 22:28:43


I am not entirely sure what you are trying to get at here.

The default behaviour of class loaders is to try to load classes from
the parent class loader first. If a class loader has duplicate classes
available, they will not be loaded. (There are some variations on this.
Web app servers should link webapp classes in preference to common
classes, so as to allow different versions of a library to be used
within the same JVM instance.)

If classes reference other classes that are only available from a child
class loader, than that clearly isn't going to work. How would the code
know, for instance, whether another child class loader will be created
in the future.

Classes with different class loaders can interact by reflection,
although it isn't very convenient. It's best to put as much as possible
into a common parent.

If you want classes to cyclically link to one another, then you will
need to load those classes by the same class loader.

Tom Hawtin
--
Unemployed English Java programmer
http://www.yqcomputer.com/

 
 
 

Is it possible to merge ClassLoaders?

Post by Marku » Fri, 25 Nov 2005 23:48:50

My problem is that I want to load some libs (jar-files) from inside a
jar.
Example:
Try to put a databasedriver (a jar file) and your app to a jar and now
try to use the classes from the driver in your app.
You will see that it doesn't work because simple jar-files can't load
jars which are inside them.

In J2EE-applications you can create a EAR-file with a lib-folder where
all libs (and there classes) will be loaded into classpath.
--> I try to get such a feature for jar-files.

Markus
 
 
 

Is it possible to merge ClassLoaders?

Post by Andrew Tho » Sat, 26 Nov 2005 00:30:55


Put the external JAR's in the program's classpath and the
problem is solved. If deploying using webstart, this would
be the best way to do it, as the add-in JAR's need only be
downloaded once, no matter how many applications use them.

HTH

--
Andrew Thompson
physci, javasaver, 1point1c, lensescapes - athompson.info/andrew
Currently accepting short and long term contracts - on Earth.
 
 
 

Is it possible to merge ClassLoaders?

Post by Thomas Haw » Sat, 26 Nov 2005 03:24:27


So your situation is, or is analogous to: You have a jar containing your
application. It also contains a database driver in a jar as a file
within the application jar.

The obvious solution is to put everything in the same jar. Leaving that
aside...

In the case of JDBC drivers, if you stick to the JDBC API, you should be
able to load the driver class (*initialised*) and run.

I believe Mustang does something funky where you can reference the
driver from META-INF.

If you must have your application classes link to another jar that
cannot be on the classpath, then: Write a small bootstrap that loads
both jars into a single class loader. The class loader's parent should
be your bootstraps class loader's *parent* (alternatively fiddle it so
that it loads its own classes in preference to its parent).

Tom Hawtin
--
Unemployed English Java programmer
http://www.yqcomputer.com/
 
 
 

Is it possible to merge ClassLoaders?

Post by Ferenc Hec » Sat, 26 Nov 2005 05:16:46


"Markus" < XXXX@XXXXX.COM > schrieb

You need a special class-loader like one-jar http://www.yqcomputer.com/

There is an eclipse-plugin Fat-Jar which deploys eclipse-projects into as
One-JARs http://www.yqcomputer.com/

Best regards,

feri
 
 
 

Is it possible to merge ClassLoaders?

Post by Marku » Sat, 26 Nov 2005 07:39:30

JPackIt also seems to be very good.
http://www.yqcomputer.com/

Markus
 
 
 

Is it possible to merge ClassLoaders?

Post by pkrien » Sat, 26 Nov 2005 21:33:30

Make your JARs OSGi bundles (just modifying the manifest and tell what
it imported and exports) and use the Eclipse, Felix/Oscar or
Knopflerfish framework to run your application. Class loaders are
monsters, use a standard approach!

More info: http://www.yqcomputer.com/

Kind regards,

Peter Kriens