简体   繁体   中英

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. 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. 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). 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:

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)

I've already checked that

  • the generated URL can be correctly resolved by any web browser and Windows Explorer.
  • Circle.class has been built and is present in the directory referenced by the used URL
  • className resolves to "plugin.Circle" which should be the correct binary name to use
  • removing inheritance from plugin.Circle does not make any difference.

I've pretty much run out of ideas on what else to try. What am I doing wrong?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM