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{}