| 64 | } |
| 65 | |
| 66 | public void compile(String target, JavaSourceFromString... sources) { |
| 67 | LOG.info("compiling java classes to "+target); |
| 68 | JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
| 69 | JavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); |
| 70 | DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); |
| 71 | |
| 72 | List<String> optionList = Lists.newArrayList(); |
| 73 | // Adds the current classpath to the compiler along with our generated code |
| 74 | optionList.add("-classpath"); |
| 75 | optionList.add(classPath); |
| 76 | optionList.add("-d"); |
| 77 | optionList.add(target); |
| 78 | |
| 79 | if (!compiler.getTask(null, fileManager, diagnostics, optionList, null, Arrays.asList(sources)).call()) { |
| 80 | LOG.warn("Error compiling Printing compilation errors and shutting down."); |
| 81 | for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) { |
| 82 | LOG.warn("Error on line " + diagnostic.getLineNumber() + ": " + diagnostic.getMessage(Locale.US)); |
| 83 | } |
| 84 | throw new RuntimeException("Unable to compile"); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | public void addToClassPath(String path) { |
| 89 | this.classPath = this.classPath + System.getProperty("path.separator") + path; |