When does a class of a dynamically loadable class that references non-existent classes / methods not work in Java?

Suppose I dynamically load a Java C class that references a nonexistent class / methods. Such a case may occur when C was written for a newer version of Java. When will the crash happen - as soon as C is loaded, or when a method is invoked that calls a nonexistent class / method? Does this change with the VM, including other versions of Java, such as Java ME?

+4
source share
4 answers

When will this happen?

as soon as C boots up?

No. Only if when loading it refers to a nonexistent class (i.e. you have a class attribute of this type)

or when a method is launched that calls a nonexistent class / method?

Yes. It will be so.

For example, this works well.

 C:\>more > A.java class A {} ^C C:\>more > B.java class B { public void method() { A a = new A(); } public void other() { System.out.println("Hello there"); } public static void main( String ... args ) { B b = new B(); b.other(); } } C:\>javac A.java B.java C:\>erase A.class C:\>java B Hello there 

Class B is loaded by java, but since none of the method codes used works well.

Unlike:

 C:\>more > A.java class A {} C:\>more > B.java class B { void method() { A a = new A(); } public static void main( String ... args ) { B b = new B(); b.method(); } } ^C C:\>javac A.java B.java C:\>erase A.class C:\>java B Exception in thread "main" java.lang.NoClassDefFoundError: A at B.method(B.java:3) at B.main(B.java:7) Caused by: java.lang.ClassNotFoundException: A at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 2 more 

Failed as B tried to access A.

+4
source

In at least regular old Java, I found that it would not work the moment you refer to a broken class or method:

An example from my recent history:

I am using a Java editor (written in Java) using a simple plugin system. I updated the editor, but used a plugin compiled with the old version of the editor, which still referred to the class, where for brevity we will call Foo , which was derived from the inner class and into the package by itself.

When I called the plugin, it did not fail until it tried to create an instance of Foo . Since Foo was not at the place where the plugin code was specified, he chose NoClassDefFoundError .

On the side of the note, between at least versions of Java, such as Java 1.5 and Java 1.6, you usually do not need to worry about deleting or moving something, as people will scream about a bloody murder if you mess with the installed API .

+2
source

This step is called "permission", loading other classes referenced by this class.

The language specification allows you to change when this step is performed. This can be done most willingly, at the beginning of the virtual machine, a recursive solution to all classes directly or indirectly associated with the main class. Or it can be done lazily by allowing the class only when absolutely necessary.

My experience with JVM servers is that it runs as late as possible. Thus, until the code referencing the missing classes has been executed, no errors are visible.

It also depends on how your loading of your class. java.lang.Classloader.loadClass(name) does not execute permission. However, there is a protected loadClass(name,resolve) method that you can use to force resolution at boot time. However, its exact behavior is poorly documented.

+1
source

It will not boot. This can be quite annoying - but of course, this is not enough to be careful. A way around this is to dynamically load different classes depending on what other definitions you know.

0
source

Source: https://habr.com/ru/post/1336232/


All Articles