samedi 23 janvier 2021

Java reflection load class from test package

I have this structure:

│   build.gradle
└───src
    ├───main
    │   └───java
    │          Main.java
    └───test
        └───java
               Test1.java

Main.java:

public class Main {

    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> testClass = Class.forName("test.Test1");
    }
}

In main method I want get metadata of Test1 class, which place in test package. But, I get this error:

Exception in thread "main" java.lang.ClassNotFoundException: test.Test1
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:377)
    at Main.main(Main.java:4)

How I can get access to classes in test package?

UPDATE: enter image description here enter image description here





Aucun commentaire:

Enregistrer un commentaire