Dynamic Class loading of java source [message #668479] |
Thu, 05 May 2011 19:54 |
Jamel G. Messages: 12 Registered: April 2011 |
Junior Member |
|
|
Hello,
I would like to compile and load dynamically a class from the current java project and then invoke a method.
I have found this article http://mortennobel.files.wordpress.com/2008/07/dynamichallow orld-java.pdf
It only works when the java class is not referencing any other java class. otherwise i got a compile error.
I think the main problem is in the classpath passed to the eclipse compiler:
JavaCompiler javac = new EclipseCompiler();
StandardJavaFileManager sjfm = javac.getStandardFileManager(null, null, null);
SpecialClassLoader cl = new SpecialClassLoader();
SpecialJavaFileManager fileManager = new SpecialJavaFileManager(sjfm, cl);
List<String> options = new ArrayList<String>();
options.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path")));
List compilationUnits = Arrays.asList(new MemorySource(className, halloWorldProgram));
DiagnosticListener dianosticListener = null;
Iterable classes = null;
Writer out = new PrintWriter(System.err);
JavaCompiler.CompilationTask compile = javac.getTask(out, fileManager, dianosticListener, options, classes, compilationUnits);
boolean res = compile.call();
if (res) {
return cl.findClass(className);
}
Any help will be much appreciated
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.01919 seconds