I have folder operators. In this folder, I compiled the file (one AND 4 interface statement that implements the statement). The goal is to download the entire .class file from this folder and use it in the main program. I use these entries:
File operatorFile = new File("D:\\operators");
URL operatorFilePath = operatorFile.toURL();
URL[] operatorFilePaths = new URL[]{operatorFilePath};
ClassLoader operatorsLoader = new URLClassLoader(operatorFilePaths);
Class[] operatorClass = new Class[]{ operatorsLoader.loadClass("Plus"), operatorsLoader.loadClass("Minus"),operatorsLoader.loadClass("Multiply") , operatorsLoader.loadClass("Divide") };
Then I use this element to call the methods of the .class class:
Method methodsInOperator;
Object instance;
String operatorSign;
for(Class operatorCls : operatorClass)
{
instance = operatorCls.newInstance();
methodsInOperator = operatorCls.getMethod("getSign", null);
operatorSign = (String)methodsInOperator.invoke(instance, null);
if(operatorSign.equals(elementInExpression[2]))
{
methodsInOperator = operatorCls.getMethod("calculate", new Class[] { double.class, double.class } );
output =(double)methodsInOperator.invoke(instance, firstNumber, secondNumber);
}
}
But below the statute dose does not work dynamically, and if we put another .class file in the folder, then the folder program stops working.
Class[] operatorClass = new Class[]{ operatorsLoader.loadClass("Plus"), operatorsLoader.loadClass("Minus"),operatorsLoader.loadClass("Multiply") , operatorsLoader.loadClass("Divide") };
My goal is to dynamically load all classes and check whether they implement the operator and, in accordance with the getSing () method, select the best class. Can anyone help me?
source
share