Dynamically create all objects contained in file

196 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 these .java files into a folder and run my program. My program would then instantiate each class in the folder and add it to an arrayList so that I can run their code within my own.

Is there anyway I could do this, or something like this? Note: I do not know the exact amount of students. Any help would be greatly appreciated.

Update: I have tried to implement Jegg's solution; however, my TeamMakerTester class cannot recognize the main method of my Team1 class. Here is the error:

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

Any help would be greatly appreciated, here's the code where Team1 is the code submitted by the student:

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();
        } catch (ClassNotFoundException cnfe){
            System.out.println("Error: Class not found");
        }
    }
}

public class Team1 extends Team{
    private String teamName = "Team1";
    private String move; // try removing

    public String getTeamName(){
        return teamName;
    }

    public static void main(String[] args){
        Team1 algo = new Team1();
        algo.runGame();
    }...

public class Team{}
1

There are 1 answers

1
Jegg On BEST ANSWER

Hi I am not sure this is what you want but I think this is regarding how you get all classes in the folder and once you get them then you can easily run in your program.

First, You can't ask your students using the same name of the file becuase they can't be put into the same folder.

and then you can consider using a class in spring framework called ClassPathScanningCandidateComponentProvider that can do scan the classes in the pacakge:

ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
provider.addIncludeFilter(new AssignableTypeFilter(MyClass.class));

// scan in org.example.package
Set<BeanDefinition> components = provider.findCandidateComponents("org/example/package");
for (BeanDefinition component : components)
{
    Class cls = Class.forName(component.getBeanClassName());
    // use class cls found
}

if the student has main method in their file you then call Student Class.main(arguments);

Have look at How do you find all subclasses of a given class in Java?