in src/main/java/org/apache/xmlbeans/impl/tool/CodeGenUtil.java [141:279]
public static boolean externalCompile(List<File> srcFiles, File outdir, File[] cp, boolean debug, String javacPath, String genver, String memStart, String memMax,
boolean quiet, boolean verbose, String sourceCodeEncoding) {
List<String> args = new ArrayList<>();
File javac = findJavaTool(javacPath == null ? DEFAULT_COMPILER : javacPath);
assert (javac.exists()) : "compiler not found " + javac;
args.add(javac.getAbsolutePath());
if (outdir == null) {
outdir = new File(".");
} else {
args.add("-d");
args.add(quoteAndEscapeFilename(outdir.getAbsolutePath()));
}
if (cp == null) {
cp = systemClasspath();
}
if(sourceCodeEncoding != null && !sourceCodeEncoding.isEmpty()) {
args.add("-encoding");
args.add(sourceCodeEncoding);
}
if (cp.length > 0) {
StringBuilder classPath = new StringBuilder();
// Add the output directory to the classpath. We do this so that
// javac will be able to find classes that were compiled
// previously but are not in the list of sources this time.
classPath.append(outdir.getAbsolutePath());
// Add everything on our classpath.
for (File file : cp) {
classPath.append(File.pathSeparator);
classPath.append(file.getAbsolutePath());
}
args.add("-classpath");
// bizarre. javac expects backslash escaping if we quote the classpath
args.add(quoteAndEscapeFilename(classPath.toString()));
}
if (genver == null) {
genver = "1.8";
}
args.add("-source");
args.add(genver);
args.add("-target");
args.add(genver);
args.add(debug ? "-g" : "-g:none");
if (verbose) {
args.add("-verbose");
}
addAllJavaFiles(srcFiles, args);
File clFile = null;
try {
clFile = Files.createTempFile(IOUtil.getTempDir(), "javac", ".tmp").toFile();
try (Writer fw = Files.newBufferedWriter(clFile.toPath(), Charset.defaultCharset())) {
Iterator<String> i = args.iterator();
for (i.next(); i.hasNext(); ) {
String arg = i.next();
fw.write(arg);
fw.write('\n');
}
}
List<String> newargs = new ArrayList<>();
newargs.add(args.get(0));
if (memStart != null && !memStart.isEmpty()) {
newargs.add("-J-Xms" + memStart);
}
if (memMax != null && !memMax.isEmpty()) {
newargs.add("-J-Xmx" + memMax);
}
newargs.add("@" + clFile.getAbsolutePath());
args = newargs;
} catch (Exception e) {
System.err.println("Could not create command-line file for javac");
}
try {
String[] strArgs = args.toArray(new String[0]);
if (verbose) {
System.out.print("compile command:");
for (String strArg : strArgs) {
System.out.print(" " + strArg);
}
System.out.println();
}
final Process proc = Runtime.getRuntime().exec(strArgs);
StringBuilder errorBuffer = new StringBuilder();
StringBuilder outputBuffer = new StringBuilder();
Thread out = copy(proc.getInputStream(), outputBuffer);
Thread err = copy(proc.getErrorStream(), errorBuffer);
proc.waitFor();
if (verbose || proc.exitValue() != 0) {
if (outputBuffer.length() > 0) {
System.out.println(outputBuffer.toString());
System.out.flush();
}
if (errorBuffer.length() > 0) {
System.err.println(errorBuffer.toString());
System.err.flush();
}
if (proc.exitValue() != 0) {
return false;
}
}
} catch (Throwable e) {
if (ExceptionUtil.isFatal(e)) {
ExceptionUtil.rethrow(e);
}
System.err.println(e.toString());
System.err.println(e.getCause());
e.printStackTrace(System.err);
return false;
} finally {
if (!debug && clFile != null) {
clFile.delete();
}
}
return true;
}