I created my class loader.

package ru.sberbank.school.homework8; import ru.sberbank.school.homework8.plugin.Plugin; import java.io.*; public class PluginManager extends ClassLoader { private final String pluginRootDirectory; public PluginManager(String pluginRootDirectory) { this.pluginRootDirectory = pluginRootDirectory; } @Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (name.startsWith(pluginRootDirectory)) { return findClass(name); } return super.loadClass(name); } public Class<?> findClass(String name) throws ClassNotFoundException { try { byte b[] = loadClassData(name + ".class"); return defineClass(name, b, 0, b.length); } catch (FileNotFoundException ex) { return super.findClass(name); } catch (IOException ex) { return super.findClass(name); } } // получаем байт-код класса private byte[] loadClassData(String path) throws IOException { try (InputStream is = new FileInputStream(new File(path))) { // Get the size of the file long length = new File(path).length(); if (length > Integer.MAX_VALUE) { // File is too large } // Create the byte array to hold the data byte[] bytes = new byte[(int) length]; // Read in the bytes int offset = 0; int numRead; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } // Ensure all the bytes have been read in if (offset < bytes.length) { throw new IOException("Could not completely read file " + path); } return bytes; } } public Plugin load(String pluginName, String pluginClassName) { String name = pluginRootDirectory + "\\" + pluginName + "\\" + pluginClassName; try { return (Plugin)loadClass(name).newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); } return null; } } 

I run the code

 package ru.sberbank.school.homework8; import ru.sberbank.school.homework8.plugin.Plugin; import java.io.File; public class PluginManagerTest { public static void main(String[] args) { String pluginRootDirectory = "D:\\sbt\\target\\classes\\ru\\sberbank\\school\\homework8"; PluginManager pluginManager = new PluginManager(pluginRootDirectory); Plugin plugin = pluginManager.load("plugin", "PluginImpl"); if (plugin != null) { plugin.doUseful(); } } } 

and get an error

 java.lang.ClassNotFoundException: D:\sbt\target\classes\ru\sberbank\school\homework8\plugin\PluginImpl at java.lang.ClassLoader.findClass(ClassLoader.java:530) 

What is the problem?

    2 answers 2

    You check that the class name does not begin with pluginRootDirectory and in this case delegates the download to the Java -loader. Java loader knows nothing about your class and therefore crashes.

    pluginRootDirectory contains:

     D:\sbt\target\classes\ru\sberbank\school\homework8 

    Obviously, the class name cannot begin this way.

      ClassNotFoundException occurs if we try to load a class at runtime and this class is not available for Java. Perhaps the reason is the wrong Classpath . It is possible that the application uses a completely different Classpath. It happens that one Classpath overwrites another, depending on how you start the application. In general, there was a lot of discussion on this issue, google your mistake, get a lot of answers to your question and possible solutions. Also see how the loader works and its principles: delegation, uniqueness, etc.