package org.soa.quartz.api.impl;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Stack;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import org.quartz.Job;
import org.quartz.JobExecutionException;
import org.soa.logger.SoaLogger;
import org.soa.quartz.core.QuartzManger;
public enum JdkCompiler{
INSTA;
public final String basePath = System.getProperty("user.dir")+File.separator+"src";
public void compile(java.util.List list,java.util.List list2){
for(int i=0;
i stack = new Stack<>();
stack.push(clazzPath);
// 遍历类路径
while (stack.isEmpty() == false) {
File path = stack.pop();
File[] classFiles = path.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.isDirectory() || pathname.getName().endsWith(".class");
}
});
for (File subFile : classFiles) {
if (subFile.isDirectory()) {
stack.push(subFile);
} else {
if (clazzCount++ == 0) {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
boolean accessible = method.isAccessible();
try {
if (accessible == false) {
method.setAccessible(true);
}
// 设置类加载器
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
// 将当前类路径加入到类加载器 强制将累加入当前classpath中
method.invoke(classLoader, clazzPath.toURI().toURL());
} finally {
method.setAccessible(accessible);
}
}
// 文件名称
String className = subFile.getAbsolutePath();
className = className.substring(clazzPathLen, className.length() - 6);
className = className.replace(File.separatorChar, '.');
// 加载Class类
SoaLogger.debug(QuartzManger.class,"动态加载[class:{"+className+"}]");
}
}
}
}
} catch (Exception e) {
}
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, JobExecutionException {
String src = "https://www.it610.com/article/package org.soa.quartz.api.job;"+
"public class Job5 implements org.quartz.Job {"+
"public void execute(org.quartz.JobExecutionContext context)"+
"throws org.quartz.JobExecutionException {"+
"System.out.println(\"我是动态添加的jobss\");
"+
"}"+
"}";
String src2 = "package liuyi.soa.quartz.api.job;
"+
"public class Job5 implements org.quartz.Job {"+
"public void execute(org.quartz.JobExecutionContext context)"+
"throws org.quartz.JobExecutionException {"+
"System.out.println(\"我是动态添加的jobss\");
"+
"}"+
"}";
final JdkCompiler jdkCompiler = JdkCompiler.INSTA;
jdkCompiler.compile(Arrays.asList(src,src2),Arrays.asList("org.soa.quartz.api.job.Job5","liuyi.soa.quartz.api.job.Job5") );
jdkCompiler.compile(Arrays.asList(src,src2),Arrays.asList("org.soa.quartz.api.job.Job5","liuyi.soa.quartz.api.job.Job5") );
jdkCompiler.loadClass();
final Job job = (Job)Class.forName("liuyi.soa.quartz.api.job.Job5").newInstance();
final Job job5 = (Job)Class.forName("liuyi.soa.quartz.api.job.Job5").newInstance();
job.execute(null);
job5.execute(null);
//new JdkCompiler().loadClass();
}}
【使用JDK动态编译java字符串】
推荐阅读
- Java|Java基础——数组
- 人工智能|干货!人体姿态估计与运动预测
- java简介|Java是什么(Java能用来干什么?)
- Java|规范的打印日志
- Linux|109 个实用 shell 脚本
- 程序员|【高级Java架构师系统学习】毕业一年萌新的Java大厂面经,最新整理
- Spring注解驱动第十讲--@Autowired使用
- SqlServer|sql server的UPDLOCK、HOLDLOCK试验
- jvm|【JVM】JVM08(java内存模型解析[JMM])
- 技术|为参加2021年蓝桥杯Java软件开发大学B组细心整理常见基础知识、搜索和常用算法解析例题(持续更新...)