代码
package com.test;
import java.io.*;
import java.lang.reflect.*;
import java.io.File;
/**
* @author DiZhuang com.test CompileTest.java */
public class CompileTest {
static com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
@SuppressWarnings("static-access")
public static void main(String[] args) {
System.out.println("User Dir is " + System.getProperty("user.dir")
+ "\\src\\com\\test");
try {
File file = File.createTempFile("RunTime", ".java", new File(System
.getProperty("user.dir")
+ "\\src\\com\\test"));
String filename = file.getName();
System.out.println("tempFile name is " + filename);
String classname = filename.substring(0, filename.indexOf('.'));
System.out.println("class name is " + classname);
PrintWriter out = new PrintWriter(new FileOutputStream(file));
out.println("package com.test;");
out.println("public class " + classname + "{");
out.println("public static void main(String[] args){");
out.println("System.out.println(\"Yes!\");");
out.println("}}");
out.flush();
out.close();
String[] arg = new String[] { "-d",
System.getProperty("user.dir") + "\\bin",
System.getProperty("user.dir") + "\\src\\com\\test\\"
+ filename };
int status = javac.compile(arg);
System.out.println("compile status is " + status);
Class<?> cls = Class.forName("com.test."+classname);
Method main = cls.getMethod("main", new Class[] { String[].class });
main.invoke(null, new Object[] { new String[0] });
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.test;
import java.io.*;
import java.lang.reflect.*;
import java.io.File;
/**
* @author DiZhuang com.test CompileTest.java */
public class CompileTest {
static com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
@SuppressWarnings("static-access")
public static void main(String[] args) {
System.out.println("User Dir is " + System.getProperty("user.dir")
+ "\\src\\com\\test");
try {
File file = File.createTempFile("RunTime", ".java", new File(System
.getProperty("user.dir")
+ "\\src\\com\\test"));
String filename = file.getName();
System.out.println("tempFile name is " + filename);
String classname = filename.substring(0, filename.indexOf('.'));
System.out.println("class name is " + classname);
PrintWriter out = new PrintWriter(new FileOutputStream(file));
out.println("package com.test;");
out.println("public class " + classname + "{");
out.println("public static void main(String[] args){");
out.println("System.out.println(\"Yes!\");");
out.println("}}");
out.flush();
out.close();
String[] arg = new String[] { "-d",
System.getProperty("user.dir") + "\\bin",
System.getProperty("user.dir") + "\\src\\com\\test\\"
+ filename };
int status = javac.compile(arg);
System.out.println("compile status is " + status);
Class<?> cls = Class.forName("com.test."+classname);
Method main = cls.getMethod("main", new Class[] { String[].class });
main.invoke(null, new Object[] { new String[0] });
} catch (Exception e) {
e.printStackTrace();
}
}
}
按照网上的例子改写了一下,出现问题如下:
1 说找不到com.sun.tools.javac.Main ,主要是在Eclipse下加载了自己的JRE吧,我是这么认为的,解决办法:将别的JDK下面的tools.jar装入Eclipse类路径下(个人认为这是我们写test程序最省事的办法)
2 Eclipse下编译后的代码在bin文件夹下面,并且一般都有包名,所以编译和运行时候,千万别忘记路径,注意:这个我们在JDK下运行是不一样。
修改如下:
String[] arg = new String[] { "-d",
System.getProperty("user.dir") + "\\bin",
System.getProperty("user.dir") + "\\src\\com\\test\\"
+ filename };
-d 指明编译后类的存放位置
Class<?> cls = Class.forName("com.test."+classname);
指明类名