Java Tutorial/Development/Java Compiler
Содержание
Compile Java file
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class CompileFiles2 {
public static void main(String[] args) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> dc;
dc = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager sjfm;
sjfm = compiler.getStandardFileManager(dc, null, null);
Iterable<? extends JavaFileObject> fileObjects;
fileObjects = sjfm.getJavaFileObjects(args);
compiler.getTask(null, sjfm, dc, null, null, fileObjects).call();
for (Diagnostic d : dc.getDiagnostics()) {
System.out.println(d.getMessage(null));
System.out.printf("Line number = %d\n", d.getLineNumber());
System.out.printf("File = %s\n", d.getSource());
}
}
}
Compiler Info
import java.util.Set;
import javax.lang.model.SourceVersion;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class CompilerInfo {
public static void main(String[] args) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
System.out.println("Supported source versions:");
Set<SourceVersion> srcVer = compiler.getSourceVersions();
for (SourceVersion sv : srcVer)
System.out.println(" " + sv.name());
int nargs = compiler.isSupportedOption(args[0]);
if (nargs == -1)
System.out.println("Option " + args[0] + " is not supported");
else
System.out.println("Option " + args[0] + " takes " + nargs + " arguments");
}
}
Compile String
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;
public class CompileString {
public static void main(String[] args) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String program = "class Test{" + " public static void main (String [] args){"
+ " System.out.println (\"Hello, World\");"
+ " System.out.println (args.length);" + " }" + "}";
Iterable<? extends JavaFileObject> fileObjects;
fileObjects = getJavaSourceFromString(program);
compiler.getTask(null, null, null, null, null, fileObjects).call();
Class<?> clazz = Class.forName("Test");
Method m = clazz.getMethod("main", new Class[] { String[].class });
Object[] _args = new Object[] { new String[0] };
m.invoke(null, _args);
}
static Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {
final JavaSourceFromString jsfs;
jsfs = new JavaSourceFromString("code", code);
return new Iterable<JavaSourceFromString>() {
public Iterator<JavaSourceFromString> iterator() {
return new Iterator<JavaSourceFromString>() {
boolean isNext = true;
public boolean hasNext() {
return isNext;
}
public JavaSourceFromString next() {
if (!isNext)
throw new NoSuchElementException();
isNext = false;
return jsfs;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}
class JavaSourceFromString extends SimpleJavaFileObject {
final String code;
JavaSourceFromString(String name, String code) {
super(URI.create("string:///" + name.replace(".", "/") + Kind.SOURCE.extension), Kind.SOURCE);
this.code = code;
}
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return code;
}
}
Enum Alternate Java Compilers
import java.util.ServiceLoader;
import javax.tools.JavaCompiler;
public class EnumAlternateJavaCompilers {
public static void main(String[] args) {
ServiceLoader<JavaCompiler> compilers;
compilers = ServiceLoader.load(JavaCompiler.class);
System.out.println(compilers.toString());
for (JavaCompiler compiler : compilers)
System.out.println(compiler);
}
}
Get classpath using System class
public class Main {
public static void main(String[] args) {
System.out.println(System.getProperty("java.class.path"));
}
}
JavaCompiler.run to compile
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class CompileFiles1 {
public static void main(String[] args) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, args);
}
}
Programmatically compile Java class
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Main {
public static void main(String[] args) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null, "Hello.java");
System.out.println("Compile result code = " + result);
}
}