I have a List of class names and class source code. I need to compile in memory these classes so I can use them in the program. Compiling a class is fine, except when that class requires another class that has to be compiled. For example, if I have class A
package example;
public class A {
public A() {
doSomething();
}
}
The class works fine, however if I have to compile this class after it:
package example;
public class B {
private A holderForA;
public B() {
this.holderForA = new A();
}
}
B will not successfully compile.
Here is my compilation code (code is the list of code mentioned before) with the two classes.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
for(String key : code.keySet()) {
CODE = code.get(key);
NAME = key;
JavaFileObject file = new JavaSourceFromString(NAME, CODE);
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnits);
System.out.println("The task completed: " + task.call() + " for " + NAME);
}
The first class returns true, the second returns false. If I set up multiple classes like A and B, A type classes work, B type classes fail. What do I do?
I believe your problem is in the JavaSourceFromString class, which needs to encode a URI for the class name: