简体   繁体   English

URLClassLoader不断抛出ClassNotFoundException。 我究竟做错了什么?

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

I'm working on a university java project that requires me to implement a simple plugin architecture. 我正在一个大学的Java项目中工作,该项目要求我实现一个简单的插件体系结构。 A main application in Project A would have to load plugins from a specified directory located in another project B. After having done some research on the topic (including stackoverflow), I decided to go with URLClassLoader to load the classes which I might then instatiate. 项目A中的一个主要应用程序必须从另一个项目B中的指定目录中加载插件。在对该主题进行了一些研究(包括stackoverflow)之后,我决定使用URLClassLoader来加载我可能随后会实例化的类。 Project B references Project A and all plugins extend a common plugin class (might as well be an interface but should not make any difference). 项目B引用了项目A,并且所有插件都扩展了一个通用的插件类(也可以是一个接口,但不应有任何区别)。 This is what I have got so far: 到目前为止,这是我得到的:

The source for the plugin class I try to load: 我尝试加载的插件类的源代码:

package plugin;

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

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

   }
}

and the code that is supposed to load the class: 以及应该加载该类的代码:

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

Now I always end up with a ClassNotFoundException: 现在,我总是以ClassNotFoundException结尾:

java.lang.ClassNotFoundException: plugin.Circle 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 de.uks.student.pluginpattern.model.EditorSystem.init(EditorSystem.java:412) at de.uks.student.pluginpattern.EditorSystem.main(EditorSystem.java:13) 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)

I've already checked that 我已经检查过了

  • the generated URL can be correctly resolved by any web browser and Windows Explorer. 生成的URL可以通过任何Web浏览器和Windows资源管理器正确解析。
  • Circle.class has been built and is present in the directory referenced by the used URL Circle.class已构建,并存在于所用URL引用的目录中
  • className resolves to "plugin.Circle" which should be the correct binary name to use className解析为“ plugin.Circle”,它应该是要使用的正确二进制名称
  • removing inheritance from plugin.Circle does not make any difference. 从plugin.Circle删除继承没有任何区别。

I've pretty much run out of ideas on what else to try. 我几乎没有其他尝试方法的想法。 What am I doing wrong? 我究竟做错了什么?

我必须将ClassLoader指向bin文件夹而不是bin / plugins文件夹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnmarshalException和ClassNotFoundException:我在做什么错? - UnmarshalException and ClassNotFoundException: what am I doing wrong? MysqlDataSource的Java EE ClassNotFoundException。 我究竟做错了什么? - Java EE ClassNotFoundException for MysqlDataSource. What am I doing wrong? Ormlite在create()上抛出SQLException。 我究竟做错了什么? - Ormlite throwing SQLException on create(). What am I doing wrong? 我究竟做错了什么?? - What am I doing wrong?? 我做错了什么 - What am i doing wrong with this 我正在尝试使用parse注册用户,但它不断抛出JSONException,这可能是什么问题? - I am trying to register a user with parse but it keeps throwing JSONException, what could be wrong? 这个析因程序有什么问题? 它只是继续返回原始值 - What am I doing wrong with this factorial program? It just keeps on returning back the original value ArrayList在循环后保持重置状态。 我究竟做错了什么? - ArrayList keeps resetting after loop. what am I doing wrong? Google Domain API会不断返回401/403作为响应。 我究竟做错了什么? - Google Domain API keeps returning 401/403 in responses. What am I doing wrong? 我在这里用选择排序做错了什么。 不断给我有关类型的问题 - What am I doing wrong here with the selection sort. Keeps giving me issues about the type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM