Phoenix
Phoenix

Reputation: 8923

How can I detect whether the JVM I am running on is using multiple classloaders?

I think my question is self-explanatory. What will be the quickest and most accurate method of determining that the JVM I use is using multiple class loaders? If it does, What care must be taken to ensure that programs run correctly?

EDIT: I think I needed some clarification around the fact that two classloaders, could they load the same class ? In case of a class like singleton where only a single instance is required how can we prevent it from happening ?

Upvotes: 1

Views: 299

Answers (3)

Peter Lawrey
Peter Lawrey

Reputation: 533472

Every JVM uses multiple class loaders.

java -XX:+TraceClassLoading -version 2>&1 | cut -d' ' -f2 | grep ClassLoader

prints

java.lang.ClassLoader
sun.reflect.DelegatingClassLoader
java.lang.ClassLoader$3
java.lang.ClassLoader$NativeLibrary
java.security.SecureClassLoader
java.net.URLClassLoader
sun.misc.Launcher$ExtClassLoader
java.lang.ClassLoader$ParallelLoaders
java.net.URLClassLoader$7
sun.misc.Launcher$ExtClassLoader$1
sun.misc.Launcher$AppClassLoader
sun.misc.Launcher$AppClassLoader$1
java.lang.SystemClassLoaderAction

Some of these are inner classes and some extend each other, but there is more than one ClassLoader here and all I did was ask for the version of the JVM.

Upvotes: 1

Trent Gray-Donald
Trent Gray-Donald

Reputation: 2346

I'm struggling with the question.

Every JVM is running multiple classloaders all the time: system classloader, extension classloader, and application classloader at the very least. OSGi, etc.. can add many, many more.

I'm struggling to see any problems here - the whole point of classloaders is isolating namespaces.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168815

What care must be taken to ensure that programs run correctly?

For classes, none that I know of. For other application resources (images, help files etc.), use the context class loader.

Upvotes: 2

Related Questions