Today I asked many questions about Jars & JavaCompiler. Because I haven't been able to achieve my goal I'll ask the whole question now:
I want a ProgramOne.jar which compiles a class out of a file and than loads this class.
Somehow I can't achieve that with the approach which works in Eclipse (the ProgramOne not packed into a Jar).
So the approach which works in eclipse is:
File fRun = new File("someFile.java");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<String> options = Arrays.asList( new String[] { "-d", currentDir+"\\bin\\"} );
Iterable<? extends JavaFileObject> compUnits = fileManager.getJavaFileObjects(fRun);
Boolean compRes = compiler.getTask(null, fileManager, null, options, null, compUnits).call();
if(compRes == true){
System.out.println("Compilation has succeeded");
fileManager.close();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class<?> compiledClass = cl.loadClass(someFile);
cRun = compiledClass;
}else{
System.out.println("Compilation error");
fileManager.close();
throw new Exception("Compilation Error");
}
This creates the class file in the folder <...>/ProgramOne/bin
and then loads the class
How can I achieve that with the packed ProgramOne.jar?