Dynamically creating and using classes in a folder

99 views Asked by At

I am developing a program to mark algorithms submitted by a group of students; each student will submit their own .java files.

I would like to place all these files (Whose names I don't know) into a folder. My program would then instantiate each of these classes, add them to an array list so I can call the main method of each class and mark the output.

I was told to use the spring frameWork (shown below); however, I am still unable to access the main method. Here's the error:

error: cannot find symbol
    cls.main();
       ^
  symbol:   method main()
  location: variable cls of type Class
1 error

Is there something I have missed? Any help would be greatly appreciated.

public class TeamMakerTester{
public static void main(String[] args){
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
    provider.addIncludeFilter(new AssignableTypeFilter(Team.class));

    // scan in org.example.package
    Set<BeanDefinition> components = provider.findCandidateComponents("org/mysteryhobo/algorithms");
    for (BeanDefinition component : components)
    {
        try{
            Class cls = Class.forName(component.getBeanClassName());
            cls.main(); //here
        } catch (ClassNotFoundException cnfe){
         System.out.println("Error: Class not found");
        }
    }
}

Solved by Erwin Bolwidt:

    public class TeamMakerTester{
        public static void main(String[] args){
        ClassPathScanningCandidateComponentProvider provider = new   ClassPathScanningCandidateComponentProvider(true);
        provider.addIncludeFilter(new AssignableTypeFilter(Team.class));

        // scan in org.example.package
        Set<BeanDefinition> components = provider.findCandidateComponents("org/mysteryhobo/algorithms");
        for (BeanDefinition component : components){
            try{
                Class<?> cls = Class.forName(component.getBeanClassName());
            //cls.main();
            Method m = cls.getMethod("main", String[].class);
            String[] params = {}; // Insert any arguments that you want to pass
            m.invoke(null, (Object) params);
            }catch (ClassNotFoundException cnfe){
                System.out.println("Error: Class not found");
            }catch(NoSuchMethodException me){
                System.out.println("Error: Method does not exist");
            }catch (IllegalAccessException iae){
                System.out.println("Error: Denied access to method");
            }catch (InvocationTargetException ite){
                System.out.println("Error: Invalid target method");
            }
        }
    System.out.println("Test");
    }
}
1

There are 1 answers

0
Erwin Bolwidt On BEST ANSWER

Did you look at the Javadoc for java.lang.Class ? There's no method called main in Class, so you cannot invoke it that way.

If you want to invoke a static method on a class that you don't know yet at compile time, then you need to use the reflection API.

Replace cls.main(); //here with:

Method m = cls.getMethod("main", String[].class);
String[] params = {}; // Insert any arguments that you want to pass
m.invoke(null, (Object) params);

You'll need to handle a number of checked exceptions as well.