Hello!
in my Webapp I am trying to compile dynamically generated source files with the Compiler API:
JavaCompiler compilerTool = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager manager = compilerTool.getStandardFileManager(null,null,null);
//directory is the dir with the source files
List<File> fileList = Arrays.asList(directory.listFiles());
Iterable<? extends JavaFileObject> units = manager.getJavaFileObjectsFromFiles(fileList);
//set output dir and classpath
List<String> options = new ArrayList<String>();
options.add("-d");
options.add("myAppPath/WEB-INF/classes/");
options.add("-cp");
options.add("myAppPath/WEB_INF/lib/test.jar;myAppPath/WEB_INF/lib/test2.jar");
CompilationTask task = compilerTool.getTask(null, manager, null, options, null, units);
task.call();
manager.close();
because the classes import other classes used in the webapp I have to change the classpath and the output dir should be the classes dir of my webapp. As you can see above I try to set compiler options but that doesn�t work, I always get:
java.lang.IllegalArgumentException: invalid flag: -d
at com.sun.tools.javac.api.JavacTool.processOptions(JavacTool.java:236)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:207)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:53)
I tried the same in a standalone java application and everything worked fine...
maybe anyone got an idea how to set the output and classpath flags right?