dimanche 3 avril 2016

Reflection API - ClassNotFound Exception

Scenario: I have java classes in the folder in a drive. I want to invoke a method, from a specific class, say Regex. Can I use Reflection API to invoke method ? I have the following code:

File dir = new File("H:/TestFolder/sample");
    File[] directoryListing = dir.listFiles();
    if (directoryListing != null) {
        for (File child : directoryListing) {
            File[] dirListing = child.listFiles();
            for (File fileName : dirListing) {
                if (fileName.getName().equals("Regex.java")) {
                    Class<?> clazz;
                    try {

                        clazz = Class.forName("Regex");
                        Constructor<?> ctor = clazz.getConstructor();
                        Object regexClass = ctor.newInstance();
....}

I get ClassNotFoundException. Can someone tell me what am I doing wrong?





Aucun commentaire:

Enregistrer un commentaire