Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do people use class loading for?

So, every Java text book talks about how flexible Java is since it can load classes at run time. Just cobble together a string and give it to Class.forName(), and catch the ClassNotFoundException and handle it. So much for the theory.

Can you give examples of how you've been using Java class loading to achieve a feature which otherwise wouldn't have been possible or easy? Note that I'm not asking "what great things could we do?" - I'm looking for real-world examples, be it an open-source application or - if you can describe this without giving out too much details - a proprietary application.

Edit: Sure, the VM loads classes lazily as it needs them. That's a behind-the-scenes thing as long as I'm sure that all classes I'll ever need are there. How do I handle a ClassNotFoundException? Suppose I've written ten pages worth of text, and the PrinterDriver class can't be found.

like image 560
doppelfish Avatar asked Feb 01 '10 06:02

doppelfish


People also ask

What happens when class is loaded?

Class loading is done by ClassLoaders in Java which can be implemented to eagerly load a class as soon as another class references it or lazy load the class until a need of class initialization occurs. If Class is loaded before its actually being used it can sit inside before being initialized.

Which Classloader is used to load a class?

Types of ClassLoaders in Java To know the ClassLoader that loads a class the getClassLoader() method is used. All classes are loaded based on their names and if any of these classes are not found then it returns a NoClassDefFoundError or ClassNotFoundException.

How class loading happens in Java?

Java ClassLoader is used to load the classes at run time. In other words, JVM performs the linking process at runtime. Classes are loaded into the JVM according to need. If a loaded class depends on another class, that class is loaded as well.

What is the use of custom ClassLoader in Java?

Java uses ClassLoader implicitly when you use new , import keyword, the jvm will use the current class's classloader to load the dependent classes, so you can use the custom classloader to load a bootstrap class explicitly by using classloader.


4 Answers

Plugins is the first thing that comes to mind. Java class loading makes it very easy compared to languages like C++.

One point that you may not be aware of is that any Java virtual machine heavily relies on class loading internally. Everytime a reference to, say, a method is seen by the bytecode interpreter, it checks whether the class the method belongs to is already loaded, and if it is not, loads it using the very same mechanism behind Class.forName() before resolving the method. This mecanism is very powerful as any Java application truly acts as a set of replaceable components which are all dynamically loaded. If the VM is well written it can for instance load classes through a custom class loader that fetches classes from the network instead of regular files.

The class loading time depends on the virtual machine implementation, but most rely on this late-binding mechanism that loads a class the first time the VM meets it.

like image 179
Gnurou Avatar answered Sep 28 '22 22:09

Gnurou


Application Servers rely also heavily on ClassLoaders to isolate the different deployed module. E.g.

  • you can deploy the same web app twice under different path
  • two applications can depend on two different version of the same library without conflict.

Thanks to the magic of class loaders...

like image 21
ewernli Avatar answered Sep 26 '22 22:09

ewernli


"PLUGIN" and that is the big word.

Basically, you can load a class that you do not know when or does not exist when you write and compile your program.

For example, if you want a program to do spell check, you can write an interface SpellChecker then load a class from a configuration file that implement the SpellChecker interface. After that, you can write any SpellChecker and set in the configuration file the actual file name. This way, your program does not need to know what class will do the spell checking.

DB driver, Eclipse's plugin, Script language, Cryptography methods are all done this way as the original writer does not know (and in some case, has no idea) what class will actually be used.

Hope this helps.

like image 6
NawaMan Avatar answered Sep 28 '22 22:09

NawaMan


Well, I've used it for dynamically loading JDBC drivers into a J2EE application. Wheteher this could have been done a better way, I have no idea.

It was just easier at the time to do the forName() call.

like image 3
paxdiablo Avatar answered Sep 26 '22 22:09

paxdiablo