Java – how do I load all compiled classes from a folder?

I have a folder operator In this folder, I have compiled files (an interface operator and class 4 implementation operator) The purpose is to load all files from this folder Class file and use it in the main program I use this statement:

File operatorFile = new File("D:\\operators");
    URL operatorFilePath = operatorFile.toURL();          
    URL[] operatorFilePaths = new URL[]{operatorFilePath};
    ClassLoader operatorsLoader = new urlclassloader(operatorFilePaths);

 //Plus,Minus,Multiply,Divide are classes that implement operator interface
   Class[] operatorClass = new Class[]{ operatorsLoader.loadClass("Plus"),operatorsLoader.loadClass("Minus"),operatorsLoader.loadClass("Multiply"),operatorsLoader.loadClass("Divide") };

Then I use this statement to call Class file method:

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 the following statement can't work dynamically if we put another one The class file is placed in the operator folder and the program stops working

Class[] operatorClass = new Class[]{ operatorsLoader.loadClass("Plus"),operatorsLoader.loadClass("Divide") };

My goal is to dynamically load all classes and check whether they implement operators, and select the best class according to the getsing () method Can anyone help me?

Solution

There are two parts:

>First list the files in a given folder. > Then load the class if it is a given interface / type

I've written a little way to do this - you can follow your own logic

public Class[] getOperators(File operatorFile) throws MalformedURLException,ClassNotFoundException {
    ClassLoader operatorsLoader = new urlclassloader(new URL[] { operatorFile.toURI().toURL() });

    File[] files = operatorFile.listFiles(new FilenameFilter() {
        @Override public boolean accept(File dir,String name) {
            return name.endsWith(".class");
        }
    });
    ArrayList<Class> operators = new ArrayList<>();
    for (File file : files) {
        String className = file.getName().substring(0,file.getName().length() - 6);
        Class<?> clazz = operatorsLoader.loadClass(className);
        if(OperatorInterface.class.isAssignableFrom(clazz)) {
            operators.add(clazz);
        }
    }
    return operators.toArray(new Class[operators.size()]);
}

If all goes well, calling this method using the folder should return all classes that implement operatorinterface in the folder I didn't execute it - so you may have to solve some problems with the code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>