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?