Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically load a Java class without using a class loader?

I recently saw an online presentation of JRebel in which they explain what their product does, in one of the slides they mention they don't use class loaders (see screenshot below).

I have some projects in which I use class loaders for dynamically loading classes at runtime, for example for dependency injection (like in the Maker Factory framework that I developed). I thought the only way to load a class in Java was by using a class loader.

enter image description here

like image 512
raspacorp Avatar asked Jul 23 '14 20:07

raspacorp


People also ask

Can JVM exist without a class loader?

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 do you load classes which are not in your classpath?

You can create an instance of URLClassLoader to load new classes from a directory: URL dirUrl = new URL("file:/" + "path_to_dir" + "/"); // 1 URLClassLoader cl = new URLClassLoader(new URL[] {dirUrl}, getClass().

What takes a string class name and loads the necessary class dynamically at run time?

Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Let us look at how to load classes dynamically by using Class. forName (String className); method, it is a static method. The above static method returns the class object associated with the class name.


3 Answers

They don't use any new classloaders; they extend the default ones.

https://www.jrebel.com/learn/how-does-jrebel-work

like image 114
Dave Newton Avatar answered Nov 11 '22 09:11

Dave Newton


Disclaimer: I'm involved with JRebel development

Technically, it is possible to bypass the classloader with some Unsafe magic, and JVM leverages that with lambdas when creating the runtime anonymous classes (in Java 8).

However, JRebel actually integrates with the existing classloaders and doesn't create new ones - that's what the slide mean. JRebel doesn't drop the existing classloader when it has to reload a class. Instead, it loads and versions the classes within the existing classloaders.

like image 44
Anton Arhipov Avatar answered Nov 11 '22 09:11

Anton Arhipov


they mention they don't use class loaders.

Every class uses a ClassLoader (except primitives). However, a library doesn't have to create additional ClassLoader.

Even a "Hello World" program will have two class loaders (one for boostrapping)

I thought the only way to load a class in Java was by using a class loader.

It is, but you can force the existing classloader to load your class which is hack but might be simpler to use.

(Correction) If you use null as a ClassLoader, Unsafe.defineClass() will default to the ClassLoader of the caller.

like image 39
Peter Lawrey Avatar answered Nov 11 '22 08:11

Peter Lawrey