Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading classes and resources in Java 9

I was reading this article on InfoQ quoting Reinhold:

Developers can still use the Java class path in Java 9 for the Java runtime to search for classes and resource files. It's just that with Java 9's modules, developers no longer need the class path.

So now my question is: what is the proper Java 9 way to do the tasks listed above? How do you dynamically load e.g. an image (short of fiddling with relative paths)?

Even more interestingly, how would one check if a class is available and make a decision dynamically (e.g. check if Jackson is available and, if so, use it for JSON serialization and if not use something else)?

The article also mentions Spring Boot already supporting Java 9, and Spring Boot definitely does a lot of dynamic loading. So maybe someone knows the priece of code from Spring that I can look at?

like image 557
kaqqao Avatar asked Jul 18 '17 12:07

kaqqao


People also ask

What is loading class in Java?

The Java ClassLoader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. The Java run time system does not need to know about files and file systems because of classloaders. Java classes aren't loaded into memory all at once, but when required by an application.

What is loading in Java?

The Java Virtual Machine dynamically loads, links and initializes classes and interfaces. Loading is the process of finding the binary representation of a class or interface type with a particular name and creating a class or interface from that binary representation.

What is the role of ClassLoader in Java?

Class loaders are responsible for loading Java classes dynamically to the JVM (Java Virtual Machine) during runtime. They're also part of the JRE (Java Runtime Environment). Therefore, the JVM doesn't need to know about the underlying files or file systems in order to run Java programs thanks to class loaders.

What happens if the class is not loading in Java?

If the loading of a class is unsuccessful, it loads classes from jre/lib/ext directory or any other directory as java.ext.dirs. It is implemented by sun.misc.Launcher$ExtClassLoader in JVM. System Class Loader: It loads application specific classes from the CLASSPATH environment variable.

What is the use of loadclass() method in Java?

The loadClass () method calls for findLoadedClass () method to check that the class has been already loaded or not. It is required to avoid loading the class multiple times. If the class is already loaded, it delegates the request to parent ClassLoader to load the class.

How classloader loads class in Java using delegation?

If the ClassLoader is not finding the class, it invokes the findClass () method to look for the classes in the file system. The following diagram shows how ClassLoader loads class in Java using delegation. Suppose that we have an application specific class Demo.class. The request for loading of this class files transfers to Application ClassLoader.

Why do we need a classloader in Java?

If a loaded class depends on another class, that class is loaded as well. When we request to load a class, it delegates the class to its parent. In this way, uniqueness is maintained in the runtime environment. It is essential to execute a Java program. Java ClassLoader is based on three principles: Delegation, Visibility, and Uniqueness.


1 Answers

First, to set the record straight, I neither said nor wrote the text quoted above. I’d never have put it that way. That’s just sloppy reporting on the part of the publications involved.

The most important thing to understand about class loading and resource lookup in Java 9 is that, at a fundamental level, they have not changed. You can search for classes and resources in the same way that you always have, by invoking Class::forName and the various getResource* methods in the Class and ClassLoader classes, regardless of whether your code is loaded from the class path or the module path. There are still three built-in class loaders, just as there were in JDK 1.2, and they have the same delegation relationships. Lots of existing code therefore just works, out-of-the-box.

There are some nuances, as noted in JEP 261: The concrete type of the built-in class loaders has changed, and some classes formerly loaded by the bootstrap class loader are now loaded by the platform class loader in order to improve security. Existing code which assumes that a built-in class loader is a URLClassLoader, or that a class is loaded by the bootstrap class loader, may therefore require minor adjustments.

A final important difference is that non-class-file resources in a module are encapsulated by default, and hence cannot be located from outside the module unless their effective package is open. To load resources from your own module it’s best to use the resource-lookup methods in Class or Module, which can locate any resource in your module, rather than those in ClassLoader, which can only locate non-class-file resources in the open packages of a module.

like image 178
Mark Reinhold Avatar answered Sep 21 '22 13:09

Mark Reinhold