@Before public void compileClass() throws IOException { Set<File> classes = searchFile(new SuffixFilter("class"), dir); Map<String, File> name2file = new Hashtable<>(classes.size()); for (File classFile : classes) { name2file.put(classFile.getAbsolutePath(), classFile); } Set<File> srcs = searchFile(new SuffixFilter("java"), dir); StringBuilder names = new StringBuilder(), paths = new StringBuilder(); for (File src : srcs) { String classLocation = src.getAbsolutePath().substring(0, src.getAbsolutePath().length() - "java".length()) + "class"; if (name2file.containsKey(classLocation)) { if (name2file.get(classLocation).lastModified() > src.lastModified()) { System.out.println("Cached: " + src.getName()); continue; } } names.append(" ").append(src.getName()); paths.append(" ").append(src.getAbsolutePath()); } String clazzes = paths.toString(); if (!clazzes.equals("")) { System.out.println("Compile: " + names.toString()); BatchCompiler.compile( "-classpath rt.jar -g -warn:-unused -noExit -1.8 " + clazzes + " -d " + dir.getAbsolutePath(), new PrintWriter(System.out), new PrintWriter(System.err), null); } }
/** * Executes the compilation. */ public void compile() { final StringBuilder argumentBuilder = new StringBuilder(); argumentBuilder.append("-d \"").append(this.targetFolder).append("\" "); for (final String segment : this.classPath) { argumentBuilder.append("-classpath \"").append(segment).append("\" "); } if (this.charset != null) { argumentBuilder.append("-encoding ").append(this.charset).append(" "); } argumentBuilder.append("\"").append(this.sourceFilesFolder).append("\" "); argumentBuilder.append("-").append(COMPLIANCE_LEVEL); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); final boolean success = BatchCompiler.compile(argumentBuilder.toString(), new PrintWriter(outputStream), new PrintWriter(outputStream), null); if (!success) { final FailureReport<Void> failure = new FailureReport<Void>().message("The compilation failed") .details("Compilation Output: %s\n\nUsed command line options: %s\n", outputStream, argumentBuilder) .retryWith(this::compile); FAILURE_HANDLER.handle(failure); } }
public static void compile(final String file) { clear(); Content.editor.clearIcons(); StatusBar.error.setForeground(Color.black); StatusBar.error.setText("Errors"); StatusBar.warning.setForeground(Color.black); StatusBar.warning.setText("Warnings"); errorCount = 0; warningCount = 0; consoleArea.append("Compiling... "+file+".java"+System.lineSeparator()); final String buildArgs = Content.getProject()+"source/"+ file+".java" +" -cp "+classPath +" -d "+Content.getProject()+"bin" +" -1.7" +" -Xemacs"; Thread t = new Thread(new Runnable(){ @Override public void run() { BatchCompiler.compile(buildArgs, compilerWriter, compilerWriter, prog); consoleArea.append("Finished Compiling... "+file+".java"+System.lineSeparator()); System.gc(); } }); t.start(); }
public static void build() { clear(); consoleArea.append("Building.. "+Content.getProject()+System.lineSeparator()); final String buildArgs = Content.getProject()+"source/" +" -cp "+classPath +" -d "+Content.getProject()+"bin" +" -1.7" +" -Xemacs"; Thread t = new Thread(new Runnable(){ @Override public void run() { BatchCompiler.compile(buildArgs, compilerWriter, compilerWriter, prog); System.gc(); } }); t.start(); }
/** * Invokes the ECJ compiler with the given arguments. * * @param args * the arguments passed through to the compiler * @param out * get the output from System.out * @param err * get the output from System.err * @return a return code as defined by this class */ public int compile(String[] args, PrintWriter out, PrintWriter err) { /* Give me something to do, or print usage message */ if (args == null || args.length == 0) { BatchCompiler.compile("-help", out, err, null); //$NON-NLS-1$ return RC_USAGE_ERROR; } /* Add in the base class library code to compile against */ String[] newArgs = addLocalArgs(args); /* Invoke the compiler */ boolean success = BatchCompiler.compile(newArgs, out, err, null); return success ? RC_SUCCESS : RC_COMPILE_ERROR; }
@Override public CompilationResult compile(Iterable<String> sourceRoots, File outputClassDirectory) { Iterable<String> validSourceRoots = IterableExtensions.filter(sourceRoots, new EmptyOrMissingFilter()); if (!containsJavaFiles(validSourceRoots)) { return CompilationResult.SKIPPED; } List<String> commandLine = Lists.newArrayList(); if (configuration.isVerbose()) { commandLine.add("-verbose"); } if (classPath != null) { Iterable<String> validClasspath = IterableExtensions.filter(classPath, new EmptyOrMissingFilter()); if (validClasspath.iterator().hasNext()) { commandLine.add("-cp \"" + concat(File.pathSeparator, Lists.newArrayList(validClasspath)) + "\""); } } commandLine.add("-d \"" + outputClassDirectory.toString() + "\""); commandLine.add("-source " + configuration.getSourceLevel()); commandLine.add("-target " + configuration.getTargetLevel()); commandLine.add("-proceedOnError"); for (String src : validSourceRoots) { commandLine.add("\"" + src + "\""); } String cmdLine = concat(" ", commandLine); debugLog("invoke batch compiler with '" + cmdLine + "'"); boolean result = BatchCompiler.compile(cmdLine, new PrintWriter(getOutputWriter()), new PrintWriter( getErrorWriter()), null); return result ? CompilationResult.SUCCEEDED : CompilationResult.FAILED; }
@Override public final void run() { String command = getCommand(); CompilationProgress progress = getProgress(); // instantiate your subclass compiled = BatchCompiler.compile(command, new PrintWriter(out), new PrintWriter(err), progress); }
private boolean compile(String warnOpt, String annotOpt, String fileStr) { String cpStr = Strings.toSimpleString(File.pathSeparator, classpath); String cmd = String.format("-%s -classpath %s %s %s -d %s %s", javaVer, cpStr, warnOpt, annotOpt, outDir, fileStr); log.info("Executing => {}", cmd); CompilationProgress progress = new LogCompilationProgress(); StringWriter stdOut = new StringWriter(); PrintWriter out = new PrintWriter(stdOut); StringWriter errorOut = new StringWriter(); PrintWriter err = new PrintWriter(errorOut); boolean result = BatchCompiler.compile(cmd, out, err, progress); log.info("Result => [{}] {} {}", result ? "OK" : "FAIL", stdOut, errorOut); messages = parseMessages(errorOut); return result; }
public static void main(String[] args) { CompilationProgress progress = null; boolean ret = BatchCompiler.compile( "/Users/tudesheng/projects/haogrgr/haogrgr-test/src/test/java/com/haogrgr/test/main/Temp.java", new PrintWriter(System.out), new PrintWriter(System.err), progress); System.out.println(ret); }
public static void javac(final Procedure1<? super JavaCompilerParams> init) { final JavaCompilerParams params = new JavaCompilerParams(); init.apply(params); final ArrayList<String> list = CollectionLiterals.<String>newArrayList(); File _destination = params.getDestination(); boolean _equals = Objects.equal(_destination, null); if (_equals) { list.add("-d"); list.add("none"); } else { list.add("-d"); File _destination_1 = params.getDestination(); String _string = _destination_1.toString(); list.add(_string); } Collection<File> _classpath = params.getClasspath(); boolean _isEmpty = _classpath.isEmpty(); boolean _not = (!_isEmpty); if (_not) { list.add("-classpath"); Collection<File> _classpath_1 = params.getClasspath(); String _join = IterableExtensions.join(_classpath_1, ":"); list.add(_join); } Collection<File> _sources = params.getSources(); final Function1<File, String> _function = new Function1<File, String>() { public String apply(final File it) { return it.toString(); } }; Iterable<String> _map = IterableExtensions.<File, String>map(_sources, _function); Iterables.<String>addAll(list, _map); InputOutput.<String>print("compiling Java files..."); PrintWriter _printWriter = new PrintWriter(System.out); PrintWriter _printWriter_1 = new PrintWriter(System.err); final boolean result = BatchCompiler.compile(((String[]) ((String[])Conversions.unwrapArray(list, String.class))), _printWriter, _printWriter_1, null); if (result) { InputOutput.<String>println("success."); } else { InputOutput.<String>println("failed."); } }