Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - how to load different versions of the same class?

I have read a lot about Java classloaders, but so far I have failed to find an answer for this simple question:

I have two versions of com.abc.Hello.class in jars v1.jar and v2.jar. I want to use both in my application. What is the simplest way of doing this ?

I don't expect to be that simple, but something along these lines would be awesome :

Classloader myClassLoader = [magic that includes v1.jar and ignores v2.jar] Hello hello = myclassLoader.load[com.abc.Hello] 

And in a different class :

Classloader myClassLoader = [magic that includes v2.jar and ignores v1.jar] Hello hello = myclassLoader.load[com.abc.Hello] 

I would like to avoid using OSGi.

like image 753
kms333 Avatar asked Aug 01 '12 12:08

kms333


People also ask

Can a class have multiple versions of the same method?

Yes, we can define multiple methods in a class with the same name but with different types of parameters.

Can same class be loaded twice in JVM?

A class is always identified using its fully qualified name (package. classname). So when a class is loaded into JVM, you have an entry as (package, classname, classloader). Therefore the same class can be loaded twice by two different ClassLoader instances.

Can you have more than one class loader in Java?

Each application might use different versions of the same libraries, and must thus have a different classloader from the others in order to be able to have different versions of the same classes in a single JVM. but the web server has its own loader.it can have several classloaders.

How can I implement a class versioning mechanism in Java?

Implementing a class versioning mechanism while loading different bytecodes for classes with same names and packages. This can be done either through URL class loader (load jars via URLs) or custom class loaders. There are more concrete examples where custom class loaders might come in handy.

How do you load a class in Java?

An application or system class loader loads our own files in the classpath. Next, the extension one loads the Logging class. Extension class loaders load classes that are an extension of the standard core Java classes. Finally, the bootstrap one loads the ArrayList class.

How does a JVM know what version of Java a class is?

They do that "searching backwards" from their own version, meaning a Java 10 JVM looks for code in META-INF/versions/10, then META-INF/versions/9, then the root directory. These JVMs thus shadow version-unspecific class files with the newest version-specific ones they support.

What is the difference between jarjar and classloader?

Classloader will load classes from the jar that happened to be in the classpath first. Normally, incompatible versions of library will have difference in packages, But in unlikely case they really incompatible and can't be replaced with one - try jarjar. Classloaders load class on demand.


1 Answers

You're going the right way. You must take some things into account.

The normal thing is classes that exist in parent classloaders are used. So if you want two versions those classes must not be there.

But if you want to interact you can use reflection, or even better a common interface. So I'll do this:

common.jar: BaseInterface  v1.jar: SomeImplementation implements BaseInterface  v2.jar: OtherImplementation implements BaseInterface  command-line: java -classpath common.jar YourMainClass // you don't put v1 nor v2 into the parent classloader classpath  Then in your program:  loader1 = new URLClassLoader(new URL[] {new File("v1.jar").toURL()}, Thread.currentThread().getContextClassLoader()); loader2 = new URLClassLoader(new URL[] {new File("v2.jar").toURL()}, Thread.currentThread().getContextClassLoader());  Class<?> c1 = loader1.loadClass("com.abc.Hello"); Class<?> c2 = loader2.loadClass("com.abc.Hello");  BaseInterface i1 = (BaseInterface) c1.newInstance(); BaseInterface i2 = (BaseInterface) c2.newInstance(); 
like image 101
helios Avatar answered Sep 21 '22 01:09

helios