2

I am writing a program to allow my students to engage in a rudimentary AI game (similar to something IBM did years ago). The idea is pretty simple. Everyone has a project with the game jar, and their AI class MyAI.java (which implements AbstractAI). The structure is all working, they can write code into their AI class, and submit it to a common folder. The structure of the folder once a few students have submitted is:

school/stud1/MyAI.class

school/stud2/MyAI.class

I have also written code that I thought (in retrospect quite naively) would load and instantiate all of the classes into an ArrayList. The problem, is that I end up with an ArrayList of x instances of the current class.

I've found some similar questions, but the accepted answers did not work in this instance.

Some of the Loader class (not prettied up, it was just a proof of concept) is included below:

/**
* Load a single ai from a given location
* @param location  The path where the ai is: example: c:\\tourney
* @param className The complete class: "org.mrd.Tournament.MyAI"
* @return The instance of AbstractAI loaded
*/
public static AbstractAI loadAI(String location, String className){
    Object o = null;
    try {
        o = new URLClassLoader( new URL[]{new File(location).toURI().toURL()}
        ).loadClass(className).newInstance();
    } catch ...{
    }
    if (o == null) return null;
    return (AbstractAI)o;
}

/**
 * Load all current files in tournament folder.

 */
public static ArrayList<AbstractAI> loadCurrentTourneyFiles(){

      File dirs = new File("d:\\tourney\\school");
      //list of all file names
    ArrayList<String> names = new ArrayList<String>(Arrays.asList(dirs.list()));
    //Create an arraylist for all loaded AIs and load them.
    ArrayList<AbstractAI> arar = new ArrayList();
    for (String dir:names){
        arar.add(loadAI(dirs.getAbsolutePath() + "\\" + dir, "org.mrd.Tournament.MyAI"));
    }
      return arar;

}

Most relevant threads: Java ClassLoader: load same class twice Java - how to load different versions of the same class?

coder
  • 8,346
  • 16
  • 39
  • 53
Teacherd
  • 21
  • 2

1 Answers1

1

You can try to use compilation-toolbox, the idea is that you would try to load each of student jar with the following snippet:

 JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();
 JavaSourceCompiler.CompilationUnit compilationUnit = javaSourceCompiler.createCompilationUnit();
 compilationUnit.addClassPathEntry("ai_student1.jar");
 compilationUnit.addClassPathEntry("abstract_ai.jar");


  String aiProvider =  "package com.ai;\n" +
      "  import com.ai.student.AI;\n" +
        "import com.ai.AbstractAI;\n" + 
     "   public class AIProvider {\n" +
       "          public AbstractAI get() {\n" +
      "            return new AI();\n" +
      "        }\n\n" +
       "    }";

ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);
Class aIProvider = classLoader.loadClass("com.ai.Provider");
Adrian
  • 1,973
  • 1
  • 15
  • 28