mercredi 3 juin 2020

Using Java reflection to get class from class name [duplicate]

Is there a way to use Java Reflection to get class from class name without using package qualification?

The code below works when I specify the full name for Map (java.util.Map):

public class Test {
    public static void main(String args[]) {
        try {
            Class<?> clazz = Class.forName("java.util.Map");
            System.out.println(clazz.getName());
        }catch(ClassNotFoundException e) {
            System.out.println("Class Map not found");
        }
    }
}

The code below does not work and I get class not found:

public class Test {
    public static void main(String args[]) {
        try {
            Class<?> clazz = Class.forName("Map");
            System.out.println(clazz.getName());
        }catch(ClassNotFoundException e) {
            System.out.println("Class Map not found");
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire