/**
* java文件: Test.java, package: com.chenyf.entity, 所在目录:F:/today/javadir/src/main/java/com/chenyf/entity/Test.java
*/
/**
* 编译java文件,并加载其Class的工具
* dirPath: 不含包目录的java文件所在目录, F:/today/javadir/
* pakagePath: 包路径,com.chenyf.entity
*/
public static Class getClassFromJavaFile(String dirPath, String pakagePath) {
if (StringUtils.isBlank(dirPath) || StringUtils.isBlank(pakagePath)) //校验参数是否为空
return null;
String pakageDir = pakagePath.replaceAll("\.","/"); // 将路径中的 . 替换为 / , 替换后的pakageDir = com/chenyf/entity
String filePath = dirPath.concat( "/src/main/java/" ).concat( pakageDir ).concat(".java"); // src/main/java为java文件的特定目录
//编译
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
int compilationResult = javac.run(null, null, null, filePath);
if (compilationResult != 0) //compilationResult == 0,说明编译成功,在Java文件的同目录下会生成相应的class文件
throw new IllegalArgumentException("编译失败");
Class<?> clazz = null;
try {
MyClassLoader loader = new MyClassLoader(dirPath); //使用自定义ClassLoader
clazz = loader.findClass(pakagePath);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return clazz;
}
/**
*自定义ClassLoader
*/
private static final class MyClassLoader extends ClassLoader {
private String classDir; // 文件目录,例如:"file:/today/javadir/src/main/java/"
@Override
public Class<?> findClass(String name) {
String realPath = classDir + name.replace(".","/") + ".class"; //class文件的真实路径
byte[] cLassBytes = null;
Path path = null;
try {
path = Paths.get(new URI(realPath));
cLassBytes = Files.readAllBytes(path);
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
Class clazz = defineClass(name, cLassBytes, 0, cLassBytes.length); //调用父类的defineClass方法
return clazz;
}
public MyClassLoader(String classDir) {
this.classDir = "file:/".concat(classDir).concat("/src/main/java/"); //拼接 “file:/”前缀
}
}