C
cbossens73
Hi,
since a very long time I've been using a hack (I think I found
it by lurking on this group many years ago) to add .jar whose
location would only be known at runtime to the default
ClassLoader.
It was pointed out that it was an hack because it relied on:
- the default class loader being an URLClassLoader (which
apparently just happened to always happen since Java exist
but this may change)
- no security manager being installed
My old hack looked like that:
public static void defaultClassLoaderHack( final File
dirWithJarsToAdd ) {
final Method addURL;
try {
addURL = URLClassLoader.class.getDeclaredMethod( "addURL",
new Class[] {URL.class} );
addURL.setAccessible(true); //you're telling the JVM
to override the default visibility
final File[] files = getExternalJarsFromDir
(dirWithJarsToAdd);
final ClassLoader cl = ClassLoader.getSystemClassLoader();
for (File file : files) {
final URL url = file.toURI().toURL();
addURL.invoke(cl, new Object[]{url});
}
} catch (Exception e) {
e.printStackTrace();
}
}
Now I'm not interested in "fixing this hack".
I'm interested in knowing what is the correct way to
achieve what I want to achieve: make my application
find, at runtime, all the classes located inside .jar
whose location will only be known at runtime.
Would the correct (portable) way to do what I want
to do to force my application to use my own
ClassLoader, instead of ClassLoader.getSystemClassLoader()?
But how can I go about writing my application so that
all the classes are loaded into "my" URLClassLoader,
instead of adding classes to the default ClassLoader.
I'm probably way of in my understanding of class loader,
but I some things are sure:
- I cannot know where the jars will be
- I cannot package the jars with my app
- I will have a way to find, for sure, where the jars will
be at runtime
Based on that, I *think* I should use an URLClassLoader
however I've got no idea how to do this correctly.
Any pointer/help/code example much appreciated,
Charles
since a very long time I've been using a hack (I think I found
it by lurking on this group many years ago) to add .jar whose
location would only be known at runtime to the default
ClassLoader.
It was pointed out that it was an hack because it relied on:
- the default class loader being an URLClassLoader (which
apparently just happened to always happen since Java exist
but this may change)
- no security manager being installed
My old hack looked like that:
public static void defaultClassLoaderHack( final File
dirWithJarsToAdd ) {
final Method addURL;
try {
addURL = URLClassLoader.class.getDeclaredMethod( "addURL",
new Class[] {URL.class} );
addURL.setAccessible(true); //you're telling the JVM
to override the default visibility
final File[] files = getExternalJarsFromDir
(dirWithJarsToAdd);
final ClassLoader cl = ClassLoader.getSystemClassLoader();
for (File file : files) {
final URL url = file.toURI().toURL();
addURL.invoke(cl, new Object[]{url});
}
} catch (Exception e) {
e.printStackTrace();
}
}
Now I'm not interested in "fixing this hack".
I'm interested in knowing what is the correct way to
achieve what I want to achieve: make my application
find, at runtime, all the classes located inside .jar
whose location will only be known at runtime.
Would the correct (portable) way to do what I want
to do to force my application to use my own
ClassLoader, instead of ClassLoader.getSystemClassLoader()?
But how can I go about writing my application so that
all the classes are loaded into "my" URLClassLoader,
instead of adding classes to the default ClassLoader.
I'm probably way of in my understanding of class loader,
but I some things are sure:
- I cannot know where the jars will be
- I cannot package the jars with my app
- I will have a way to find, for sure, where the jars will
be at runtime
Based on that, I *think* I should use an URLClassLoader
however I've got no idea how to do this correctly.
Any pointer/help/code example much appreciated,
Charles