1

I want to be able to navigate though all folders in a folder to load the classes inside those folders

My current code doesn't check all packages. It will only load classes if they're not packaged.

 @SuppressWarnings({"rawtypes", "unchecked"})
public Class[] getClassesFromFolder() {
    File folder = getFolder();
    String thePath = folder.getPath();
    ArrayList<Class<Script>> classes = new ArrayList<Class<Script>>();
    try {

        URL[] path = {new URL("file://" + thePath + "/Scripts/")};
        File scriptFolder = new File(getFolder().getPath() + "/Scripts");
        URLClassLoader cl = new URLClassLoader(path);
        for (String script : scriptFolder.list()) {
            if (script.contains(".class") && !script.contains("$")) {
                String truePath = script.replace(".class", "");
                try {
                    Class<?> scriptClass = (Class<?>) cl
                            .loadClass(truePath);
                    classes.add((Class<Script>) scriptClass);
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }
        cl.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return classes.toArray(new Class<?>[classes.size()]);
}
  • And what specifically is your question / problem? Don't expect other people to deduct from your code what potentially had in mind; and why that isn't working. – GhostCat Mar 13 '16 at 01:42

2 Answers2

0

In order to load a Class object, you must include the package prefix. So, you will need to know where the root folder of the classes exists. In your case, it appears to be the Scripts folder.

As you navigate to subfolders, you will need to capture each subfolder name and add that to a package prefix of the Class you wish to load.

So, for example. under Scripts, if you have com/packagea/packageb/classA.class Then you would need to load it as 'com.packagea.packageb.classA'. Again assuming that Scripts is the starting point for your Classes.

pczeus
  • 7,709
  • 4
  • 36
  • 51
0

Check this code if it helps. I cannot post link directly as comment due to reputation. I have taken it from How to get all classes names in a package? - Thanks to author.

public class ClassFinder {

    private static final char PKG_SEPARATOR = '.';

    private static final char DIR_SEPARATOR = '/';

    private static final String CLASS_FILE_SUFFIX = ".class";

    private static final String BAD_PACKAGE_ERROR = "Unable to get resources from path '%s'. Are you sure the package '%s' exists?";

    public static List<Class<?>> find(String scannedPackage) {
        String scannedPath = scannedPackage.replace(PKG_SEPARATOR, DIR_SEPARATOR);
        URL scannedUrl = Thread.currentThread().getContextClassLoader().getResource(scannedPath);
        if (scannedUrl == null) {
            throw new IllegalArgumentException(String.format(BAD_PACKAGE_ERROR, scannedPath, scannedPackage));
        }
        File scannedDir = new File(scannedUrl.getFile());
        List<Class<?>> classes = new ArrayList<Class<?>>();
        for (File file : scannedDir.listFiles()) {
            classes.addAll(find(file, scannedPackage));
        }
        return classes;
    }

    private static List<Class<?>> find(File file, String scannedPackage) {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        String resource = scannedPackage + PKG_SEPARATOR + file.getName();
        if (file.isDirectory()) {
            for (File child : file.listFiles()) {
                classes.addAll(find(child, resource));
            }
        } else if (resource.endsWith(CLASS_FILE_SUFFIX)) {
            int endIndex = resource.length() - CLASS_FILE_SUFFIX.length();
            String className = resource.substring(0, endIndex);
            try {
                classes.add(Class.forName(className));
            } catch (ClassNotFoundException ignore) {
            }
        }
        return classes;
    }

    public static void main(String[] args) {
        List<Class<?>> classes = ClassFinder.find("com");
        System.out.println(classes.toString());
    }
}
Community
  • 1
  • 1
Vijay k
  • 5
  • 7