三从内存中编译
JavaCompiler不仅可以编译硬盘上的Java文件而且还可以编译内存中的Java代码然后使用reflection来运行它们我们可以编写一个JavaSourceFromString类通过这个类可以输入Java源代码一但建立这个对象你可以向其中输入任意的Java代码然后编译和运行而且无需向硬盘上写class文件
import javalangreflect*;
import javaio*;
import javaxtools*;
import javaxtoolsJavaCompilerCompilationTask;
import javautil*;
import javanet*;
public class test_compilerapi
{
private static void compilerJava() throws Exception
{
JavaCompiler compiler = ToolProvidergetSystemJavaCompiler();
DiagnosticCollector diagnostics = new DiagnosticCollector();
// 定义一个StringWriter类用于写Java程序
StringWriter writer = new StringWriter();
PrintWriter out = new PrintWriter(writer);
// 开始写Java程序
outprintln(public class HelloWorld {);
outprintln( public static void main(String args[]) {);
outprintln( Systemoutprintln(\Hello World\););
outprintln( });
outprintln(});
outclose();
//为这段代码取个名子HelloWorld以便以后使用reflection调用
JavaFileObject file = new JavaSourceFromString(HelloWorld writertoString());
Iterable compilationUnits = ArraysasList(file);
JavaCompilerCompilationTask task = compilergetTask(null null
diagnostics null null compilationUnits);
boolean success = taskcall();
Systemoutprintln(Success: + success);
// 如果成功通过reflection执行这段Java程序
if (success)
{
Systemoutprintln(输出);
ClassforName(HelloWorld)getDeclaredMethod(main new Class[]
{ String[]class })invoke(null new Object[]
{ null });
Systemoutprintln(输出 );
}
}
public static void main(String args[]) throws Exception
{
compilerJava();
}
}
// 用于传递源程序的JavaSourceFromString类
class JavaSourceFromString extends SimpleJavaFileObject
{
final String code;
JavaSourceFromString(String name String code)
{
super(URIcreate(string:/// + namereplace( /)+ KindSOURCEextension) KindSOURCE);
thiscode = code;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors)
{
return code;
}
}
[] []