Is it possible to merge ClassLoaders?

M

Markus

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
 
T

Thomas Hawtin

Markus said:
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?

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
 
M

Markus

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
 
A

Andrew Thompson

Markus said:
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?

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
 
T

Thomas Hawtin

Markus said:
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.

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
 
F

Ferenc Hechler

Markus said:
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.

You need a special class-loader like one-jar http://one-jar.sourceforge.net

There is an eclipse-plugin Fat-Jar which deploys eclipse-projects into as
One-JARs http://fjep.sourceforge.net

Best regards,

feri
 
P

pkriens

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.aqute.biz/osgi.html

Kind regards,

Peter Kriens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top