标签:
完整代码如下(用的是 eclipse 工具)
package com.dongtai.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class DynamicCompileTest {
public static void main(String[] args) throws IOException {
// 编译程序
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
int result = javaCompiler.run(null, null, null, "-d","./temp/","./temp/com/Hello.java");
System.out.println( result == 0 ? "恭喜编译成功" : "对不起编译失败");
// 运行程序
Runtime run = Runtime.getRuntime();
Process process = run.exec("java -cp ./temp temp/com/Hello");
InputStream in = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String info = "";
while ((info = reader.readLine()) != null) {
System.out.println(info);
}
}
}
package com.dongtai.demo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class DynamicCompileTest {
public static void main(String[] args) throws IOException{
// 1.创建需要动态编译的代码字符串
String nr = "\r\n"; //回车
String source = "package temp.com; " + nr +
" public class Hello{" + nr +
" public static void main (String[] args){" + nr +
" System.out.println(\"HelloWorld! 1\");" + nr +
" }" + nr +
" }";
// 2.将欲动态编译的代码写入文件中 1.创建临时目录 2.写入临时文件目录
File dir = new File(System.getProperty("user.dir") + "/temp"); //临时目录
// 如果 \temp 不存在 就创建
if (!dir.exists()) {
dir.mkdir();
}
FileWriter writer = new FileWriter(new File(dir,"Hello.java"));
writer.write(source);
writer.flush();
writer.close();
// 3.取得当前系统的编译器
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
// 4.获取一个文件管理器
StandardJavaFileManager javaFileManager = javaCompiler.getStandardFileManager(null, null, null);
// 5.文件管理器根与文件连接起来
Iterable it = javaFileManager.getJavaFileObjects(new File(dir,"Hello.java"));
// 6.创建编译任务
CompilationTask task = javaCompiler.getTask(null, javaFileManager, null, Arrays.asList("-d", "./temp"), null, it);
// 7.执行编译
task.call();
javaFileManager.close();
// 8.运行程序
Runtime run = Runtime.getRuntime();
Process process = run.exec("java -cp ./temp temp/com/Hello");
InputStream in = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String info = "";
while ((info = reader.readLine()) != null) {
System.out.println(info);
}
}
}
package com.dongtai.demo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
public class DynamicCompileTest {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException{
/*
* 编译内存中的java代码
* */
// 1.将代码写入内存中
StringWriter writer = new StringWriter(); // 内存字符串输出流
PrintWriter out = new PrintWriter(writer);
out.println("package com.dongtai.hello;");
out.println("public class Hello{");
out.println("public static void main(String[] args){");
out.println("System.out.println(\"HelloWorld! 2\");");
out.println("}");
out.println("}");
out.flush();
out.close();
// 2.开始编译
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
JavaFileObject fileObject = new JavaStringObject("Hello", writer.toString());
CompilationTask task = javaCompiler.getTask(null, null, null, Arrays.asList("-d","./bin"), null, Arrays.asList(fileObject));
boolean success = task.call();
if (!success) {
System.out.println("编译失败");
}else{
System.out.println("编译成功");
}
URL[] urls = new URL[]{new URL("file:/" + "./bin/")};
URLClassLoader classLoader = new URLClassLoader(urls);
Class classl = classLoader.loadClass("com.dongtai.hello.Hello");
Method method = classl.getDeclaredMethod("main", String[].class);
String[] argsl = {null};
method.invoke(classl.newInstance(), argsl);
}
}
引用至:http://www.cnblogs.com/snake-hand/p/3159694.html
标签:
原文地址:http://www.cnblogs.com/zxf330301/p/5533572.html