使用JDK动态编译java字符串

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字符串】

    推荐阅读