我有一个名为Operator的接口,该接口的目录是d:\ operators 接口定义如下:
package operators;
public interface Operator
{
double calculate(double firstNumber,double secondNumber);
String getSign();
}
在主程序(d:\ ProjectFile94.6.7 \ main)中我想加载这个界面并使用它。我加载界面如下:
String rootPath = System.getProperty("user.dir")+System.getProperty("file.separator");
String operatorsPath = rootPath.replace("ProjectFile94.6.7" , "").replace("main" , "") + "operators";
System.out.println(operatorsPath);
//Load operators from another file
File operatorFile = new File(operatorsPath);
URL operatorFilePath = operatorFile.toURL();
URL[] operatorFilePaths = new URL[]{operatorFilePath};
ClassLoader operatorsLoader = new URLClassLoader(operatorFilePaths);
Class operatorInterface = operatorsLoader.loadClass("operators.Operator");
应用程序编译正常,但在运行时我得到了这个例外:
java.lang.ClassNotFoundException:operators.Operator
答案 0 :(得分:0)
您不能将包名附加到类路径目录中:
String operatorsPath = rootPath.replace("ProjectFile94.6.7" , "").replace("main" , "");
即。目录路径应该只是D:/
。否则,您只是告诉类加载器搜索D:/operators/operators/Operator.class
。