0

Okay, so I'm currently scanning a folder /Scripts/ for folders, and checking those folders for .class's

The problem is when I go to load the classes in classloader, I get a error with the packaging. It is not replacing the "/" with "." like it should.

My code:

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

      try {
            Files.walk(Paths.get(thePath + "\\Scripts\\")).forEach(filePath -> {
                if (Files.isDirectory(filePath)) {
                    folders.add(filePath.toString());
                }
                if (Files.isRegularFile(filePath)) {
                    classesName.add(filePath.getFileName().toString());
                }
            });
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      ArrayList<Class<Script>> classes = new ArrayList<Class<Script>>();
      try {
        for(int i = 0; i < folders.size(); i++) {


          File scriptFolder = new File(folders.get(i).replaceAll("/", "\\\\"));
          System.out.println(scriptFolder.toString());



          URLClassLoader cl = new URLClassLoader(new URL[] {
                    new File(scriptFolder.toString())
                    .toURI().toURL() });
          for(int y = 0; y < classesName.size(); y++) {
            String script = classesName.get(y);

              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();
      }

Error:

    C:\Users\Ethan\Client\Scripts\com\ethan\test\main
Exception in thread "Thread-7" java.lang.NoClassDefFoundError: Core (wrong name: com/ethan/test/main/Core)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    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 java.lang.ClassLoader.loadClass(Unknown Source)
    at com.client.api.script.ScriptManager.getClassesFromFolder(ScriptManager.java:103)
    at com.client.ui.components.ScriptSelector.getData(ScriptSelector.java:57)
    at com.client.ui.components.ScriptSelector.<init>(ScriptSelector.java:42)
    at com.client.ui.components.sidebar.panels.RunPanel$1.run(RunPanel.java:59)
  • You're in the wrong folder, move up from `C:\Users\Ethan\Client\Scripts\com\ethan\test\main` to `C:\Users\Ethan\Client\Scripts`. – Elliott Frisch Mar 13 '16 at 06:21

1 Answers1

0

If you have a file named

C:\Users\Ethan\Client\Scripts\com\ethan\test\main\Core.class

it is likely a class named Core in package com.ethan.test.main.

If that is the case, the URLClassLoader must be given the path

C:\Users\Ethan\Client\Scripts

and the loadClass() method must be given the name

com.ethan.test.main.Core

This is exactly the same as you would need to do from the command-line, if the class had a main() method:

java  -cp C:\Users\Ethan\Client\Scripts  com.ethan.test.main.Core
Andreas
  • 154,647
  • 11
  • 152
  • 247