将包装从“/”更改为“。”

时间:2016-03-13 06:12:31

标签: java package classloader packaging urlclassloader

好的,所以我正在扫描一个文件夹/ Scripts / for files,并检查这些文件夹的.class

问题是当我在classloader中加载类时,我的包装出错了。它不是用“。”替换“/”。喜欢它。

我的代码:

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

错误:

    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)

1 个答案:

答案 0 :(得分:0)

如果您有一个名为

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

它可能是包Core中名为com.ethan.test.main的类。

如果是这种情况,必须为URLClassLoader指定路径

C:\Users\Ethan\Client\Scripts

并且必须为loadClass()方法指定名称

com.ethan.test.main.Core

如果类有main()方法,这与您在命令行中需要完全相同:

java  -cp C:\Users\Ethan\Client\Scripts  com.ethan.test.main.Core