我一直在使用Java中的简单自定义类加载器,到目前为止,一切都按预期用于非模块相关类.但是,我似乎无法找到使用我的类加载器从模块加载类的任何方法,即使模块相关的find*()方法已经重载.我试图做的是从模块"HelloModularWorld"加载一个类并运行它的主要.但是,当我指定包所在的目录时,它会"正常"加载并报告为未命名的模块.我错过了什么?
类加载器只是从文件系统的其他地方加载类,没什么特别的.
类加载器实现:
package arch.classloaders;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class ModifiableClassLoader extends ClassLoader {
private File searchPath;
//...other ctors...
public ModifiableClassLoader(File path, String name, ClassLoader parent) {
super(name, parent); //Delegate to parent classloader as required.
if (!path.isDirectory()) throw new IllegalArgumentException("Path must be a directory");
searchPath = path;
}
public void setPath(File newPath) {...}
public File getPath() {...}
//Responsible for actual loading
public Class<?> findClass(String binName) throws ClassNotFoundException {
File classfile = new File(searchPath.getPath() + File.separator …Run Code Online (Sandbox Code Playgroud) java classloader java-platform-module-system java-9 java-module