使用工具编译.java文件
public static void compileModifiedFiles(List<String> modifiedFiles, String srcRootPath, String classRootPath) {
File classRoot = new File(classRootPath);
if (!classRoot.exists()) {
classRoot.mkdir();
}
String sbUrls = System.getProperty("java.class.path");
String[] arg = new String[] { "-encoding", COMP_CHARSET, "-d", classRootPath, "-sourcepath", srcRootPath, "-cp", sbUrls.toString(),
"" };
for (String modifiedFile : modifiedFiles) {
if (null == modifiedFile || 0 == modifiedFile.length()) {
continue;
}
// 每次编译前判断该类是否已经编译过了(在其它类中有引用,则会提前编译)
//利用.class文件肯定是在.java文件之后才生成
String classFile = classRootPath + modifiedFile.substring(srcRootPath.length(), modifiedFile.length() - 5) + ".class";
if (new File(modifiedFile).lastModified() < new File(classFile).lastModified()) {
// 已经编译过,跳过
continue;
}
arg[8] = modifiedFile;
System.out.println("compiling file : " + modifiedFile);
int status = Main.compile(arg);
if (status != 0) {
throw new RuntimeException("error!");
}
}
}
javac在程序中的使用方法
最新推荐文章于 2021-02-13 17:03:50 发布