簡體   English   中英

URLClassLoader不斷拋出ClassNotFoundException。 我究竟做錯了什么?

[英]URLClassLoader keeps throwing ClassNotFoundException. What am I doing wrong?

我正在一個大學的Java項目中工作,該項目要求我實現一個簡單的插件體系結構。 項目A中的一個主要應用程序必須從另一個項目B中的指定目錄中加載插件。在對該主題進行了一些研究(包括stackoverflow)之后,我決定使用URLClassLoader來加載我可能隨后會實例化的類。 項目B引用了項目A,並且所有插件都擴展了一個通用的插件類(也可以是一個接口,但不應有任何區別)。 到目前為止,這是我得到的:

我嘗試加載的插件類的源代碼:

package plugin;

import de.uks.student.pluginpattern.model.EditorPlugin;

public class Circle extends EditorPlugin
{
   @Override
   public void init()
   {

   }
}

以及應該加載該類的代碼:

public void init(String[] args)
   {
      editorPane = new EditorPane().withWidth(500).withHeight(500);
      toolBar = new ToolBar();
      plugins = new EditorPluginSet();
      // load plugins
      File pluginDir = new File(PLUGIN_PATH);
      if (!pluginDir.exists())
      {
         System.err.println("Plugin path not found!!");
         return;
      }
      String[] plugins = pluginDir.list();
      if (plugins == null)
      {
         System.err.println("Plugin path points to a file!!");
         return;
      }

      for (String pluginString : plugins)
      {
         for (String argString : args)
         {
            if (pluginString.contains(argString))
            {
               System.out.println("Loading plugin from " + pluginString);
               File pluginFile = new File(PLUGIN_PATH + "/");
               // as getAbsolutePath embeds the relative path ... /../pluginProject ...
               // which may not be processed correctly by the OS (Windows at least),
               // we correct the path manually (just to be on the safe side)
               String absolutePath = pluginFile.getAbsolutePath();
               String[] split = absolutePath.split("\\\\");

               List<String> splitsimpleList = Arrays.asList(split);
               ArrayList<String> splitList = new ArrayList<>(splitsimpleList);
               for (int i = 0; i < splitList.size() - 1; ++i)
               {
                  if (splitList.get(i + 1).equals(".."))
                  {
                     splitList.remove(i);
                     splitList.remove(i);
                     break;
                  }
               }
               StringBuilder b = new StringBuilder();
               for (String string : splitList)
               {
                  b.append(string);
                  b.append("/");
               }
               File pluginFileForRealThisTime = new File(b.toString());

               URL pluginURL = null;
               try
               {
                  pluginURL = pluginFileForRealThisTime.toURI().toURL();
               } catch (MalformedURLException e)
               {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
               URL[] urls = {pluginURL};
               ClassLoader parentClassLoader = this.getClass().getClassLoader();
               URLClassLoader uLoader = new URLClassLoader(urls, parentClassLoader);
               Class<?> pluginClass = null;
               try
               {
                  String className = "plugin." + argString;
                  pluginClass = uLoader.loadClass(className);
               } catch (ClassNotFoundException e)
               {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
            }
         }
      }
   }

現在,我總是以ClassNotFoundException結尾:

java.lang.ClassNotFoundException:plugin.Circle在java.net.URLClassLoader $ 1.run(未知源)在java.net.URLClassLoader $ 1.run(在java.security.AccessController.doPrivileged(本機方法)處)。 net.URLClassLoader.findClass(未知源)在java.lang.ClassLoader.loadClass(未知源)在java.lang.ClassLoader.loadClass(未知源)在de.uks.student.pluginpattern.model.EditorSystem.init(EditorSystem。 java:412),位於de.uks.student.pluginpattern.EditorSystem.main(EditorSystem.java:13)

我已經檢查過了

  • 生成的URL可以通過任何Web瀏覽器和Windows資源管理器正確解析。
  • Circle.class已構建,並存在於所用URL引用的目錄中
  • className解析為“ plugin.Circle”,它應該是要使用的正確二進制名稱
  • 從plugin.Circle刪除繼承沒有任何區別。

我幾乎沒有其他嘗試方法的想法。 我究竟做錯了什么?

我必須將ClassLoader指向bin文件夾而不是bin / plugins文件夾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM