在D盘test目录下有个java文件:AlTest.java
public class AlTest {
public String sayHello(){
System.out.println("AlTest类 sayHello()方法正在执行....");
return "hello word";
}
}
现需要实现在工程已经运行过程中,进行java文件到class文件的编译操作,并运行AlTest类的方法
package com.piao.job;
import java.lang.reflect.Method;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Configurable
@EnableScheduling
public class CompilerJob {
private static final Logger logger = LoggerFactory.getLogger(CompilerJob.class);
private static boolean isExecute = false;
@Scheduled(cron = "*/10 * * * * * ")
public void test2() {
try {
if (isExecute) {
return;
}
isExecute = true; //只是测试,所以只执行一次
complierAndRun();
} catch (Exception e) {
logger.error("test", e);
}
}
public void complierAndRun(){
try {
System.out.println(System.getProperty("user.dir"));
//动态编译
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
int status = javac.run(null, null, null, "-d", System.getProperty("user.dir")+"\\target\\classes","D:/test/AlTest.java");
if(status!=0){
System.out.println("没有编译成功!");
}
//动态执行
Class clz = Class.forName("AlTest");//返回与带有给定字符串名的类 或接口相关联的 Class 对象。
Object o = clz.newInstance();
Method method = clz.getDeclaredMethod("sayHello");//返回一个 Method 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明方法
String result= (String)method.invoke(o);//静态方法第一个参数可为null,第二个参数为实际传参
System.out.println(result);
} catch (Exception e) {
logger.error("test", e);
}
}
}
运行结果:
E:\zhoufy\small\piao-admin
AlTest类 sayHello()方法正在执行....
hello word
其中代码:
int status = javac.run(null, null, null, "-d", System.getProperty("user.dir")+"\\target\\classes","D:/test/AlTest.java");
把class文件生成到了当前工程目录下的classes目录(E:\zhoufy\small\piao-admin\target\classess)所以classloader是可以加载到的,如果想知道是哪个类加载器:
Class clz = Class.forName("AlTest");//返回与带有给定字符串名的类 或接口相关联的 Class 对象。
Object o = clz.newInstance();
System.out.println(clz.getClassLoader().getSystemClassLoader());
打印的是: sun.misc.Launcher$AppClassLoader@4e0e2f2a 说明使用的是AppClassLoader
当然也可以生成到Bootstrap ClassLoader可加载的目录下
//生成到工程classes下
//int status = javac.run(null, null, null, "-d", System.getProperty("user.dir")+"\\target\\classes","D:/test/AlTest.java");
//生成到BootStrap ClassLoader可加载目录下
int status = javac.run(null, null, null, "-d", "C:\\Program Files\\Java\\jdk1.8.0_65\\jre\\classes","D:/test/AlTest.java");
当然也可以自定义类加载器,把文件生成在指定的外部目录 :
public void complierAndRun(){
try {
System.out.println(System.getProperty("user.dir"));
//动态编译
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
int status = javac.run(null, null, null, "-d", "D:\\","D:/test/AlTest.java");
if(status!=0){
System.out.println("没有编译成功!");
}
//动态执行
//Class clz = Class.forName("AlTest");//返回与带有给定字符串名的类 或接口相关联的 Class 对象。
//自定义类加载器的加载路径
MyClassLoader myClassLoader = new MyClassLoader("D:\\");
//包名+类名
Class clz = myClassLoader.loadClass("AlTest");
Object o = clz.newInstance();
Method method = clz.getDeclaredMethod("sayHello");//返回一个 Method 对象,该对象反映此 Class 对象所表示的类或接口的指定已声明方法
String result= (String)method.invoke(o);//静态方法第一个参数可为null,第二个参数为实际传参
System.out.println(result);
} catch (Exception e) {
logger.error("test", e);
}
}
java动态执行代码的代码, java eval
public class ScriptUtils {
private static final Logger logger = LoggerFactory.getLogger(ScriptUtils.class);
@SuppressWarnings("unchecked")
public static <T, E> E eval(String express, Map<String, T> params) throws ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
if(params == null){
params = new HashMap<String,T>();
}
Iterator<Map.Entry<String, T>> iter = params.entrySet().iterator();
Map.Entry<String, T> entry = null;
while(iter.hasNext()){
entry = iter.next();
engine.put(entry.getKey(), entry.getValue());
}
E result = null;
try {
result = (E)engine.eval(express);
} catch (ScriptException e) {
logger.warn("表达式执行异常: " + e.getMessage());
}
return result;
}
public static <T> Boolean evalBoolean(String express, Map<String, T> params) {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
if(params == null){
params = new HashMap<String,T>();
}
Iterator<Map.Entry<String, T>> iter = params.entrySet().iterator();
Map.Entry<String, T> entry = null;
while(iter.hasNext()){
entry = iter.next();
engine.put(entry.getKey(), entry.getValue());
}
Boolean result = null;
try {
result = (Boolean)engine.eval(express);
} catch (ScriptException e) {
result = false;
logger.warn("表达式执行异常: " + e.getMessage());
}
return result;
}
到此这篇关于java实现动态编译并动态加载的文章就介绍到这了,更多相关java动态编译内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!