I have an interface named Operator the directory of this interface is d:\\operators Interface definition is like so :
package operators;
public interface Operator
{
double calculate(double firstNumber,double secondNumber);
String getSign();
}
In the main program (d:\\ProjectFile94.6.7\\main) I want load this interface and use it .I load interface like so :
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");
The app compiled fine but at runtime I got this exception :
java.lang.ClassNotFoundException : operators.Operator
You must not append the package name in the classpath directory:
String operatorsPath = rootPath.replace("ProjectFile94.6.7" , "").replace("main" , "");
ie the directory path should be just D:/
. Otherwise, you're just telling the classloader to search for D:/operators/operators/Operator.class
.
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.