java 动态编译

时间:2022-03-21 16:44:46
public class Main {

    public static void main(String[] args) {

        System.out.println("Hello World!");
        int errorCode = com.sun.tools.javac.Main.compile(new String[] {

                "-classpath", "bin",

                "-d", "D:\\java-workspace\\forTest\\src",

                "D:\\java-workspace\\forTest\\src\\Main.java" });
    }
}

导入jdkXXX\lib\tools.jar包,运行报错:

D:\java-workspace\forTest\src\Main.java:6: 错误: 程序包com.sun.tools.javac不存在
        int errorCode = com.sun.tools.javac.Main.compile(new String[] {
                                           ^
1 个错误

改用调用系统命令启动。

http://blog.163.com/crazy_bird86/blog/static/21867375200821914340235/