0

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{}
Peter Little
  • 865
  • 10
  • 13
  • 4
    please post some code of your own. – fatCop Jun 16 '15 at 16:51
  • u can use java reflection api for this. – Bacteria Jun 16 '15 at 16:56
  • 1
    Maybe [this](http://stackoverflow.com/questions/21544446/how-do-you-dynamically-compile-and-load-external-java-classes) will provide some ideas you can use. However, if this is some student assignment, I suspect all `.java` files will have the same class names? If so, you may not be able to instantiate them all at the same time. you may have to load them in different class loaders as well, maybe? Not sure. Hope this helps. – sstan Jun 16 '15 at 17:05
  • Separate each students java files into separate directories, compile all java files in that directory. Record any compile time errors, if there were none then load all the resulting class files into a `ClassLoader` and execute the main method. – ug_ Jun 16 '15 at 17:25
  • You will be using the Reflection API for this purpose. The static factory method in class `Class` that is typically used to obtain a `Class` object for a class whose code and whose name is available at run-time but whose name was not known at compile-time is `Class.forName()`. – scottb Jun 16 '15 at 17:46

1 Answers1

1

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?

Community
  • 1
  • 1
Jegg
  • 549
  • 3
  • 11
  • Thanks for the advice, however I'm still having trouble recognizing the main from the students code. I have updated my question; is there something I have missed? – Peter Little Jun 25 '15 at 19:09