從 Java 6 版本開始,Java 語(yǔ)言就支持動(dòng)態(tài)編譯,但具體是怎么進(jìn)行動(dòng)態(tài)編譯?動(dòng)態(tài)加載的?下面,我將通過一個(gè)簡(jiǎn)單的 Java 實(shí)例來(lái)講解 Java 是如何進(jìn)行動(dòng)態(tài)編譯和動(dòng)態(tài)加載的。
在D盤test目錄下有個(gè)java文件:AlTest.java
public class AlTest {
public String sayHello(){
System.out.println("AlTest類 sayHello()方法正在執(zhí)行....");
return "hello word";
}
}
現(xiàn)需要實(shí)現(xiàn)在工程已經(jīng)運(yùn)行過程中,進(jìn)行java文件到class文件的編譯操作,并運(yùn)行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;
/**
* 任務(wù):job test
*/
@Scheduled(cron = "*/10 * * * * * ")
public void test2() {
try {
if (isExecute) {
return;
}
isExecute = true; //只是測(cè)試,所以只執(zhí)行一次
complierAndRun();
} catch (Exception e) {
logger.error("test", e);
}
}
public void complierAndRun(){
try {
System.out.println(System.getProperty("user.dir"));
//動(dòng)態(tài)編譯
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("沒有編譯成功!");
}
//動(dòng)態(tài)執(zhí)行
Class clz = Class.forName("AlTest");//返回與帶有給定字符串名的類 或接口相關(guān)聯(lián)的 Class 對(duì)象。
Object o = clz.newInstance();
Method method = clz.getDeclaredMethod("sayHello");//返回一個(gè) Method 對(duì)象,該對(duì)象反映此 Class 對(duì)象所表示的類或接口的指定已聲明方法
String result= (String)method.invoke(o);//靜態(tài)方法第一個(gè)參數(shù)可為null,第二個(gè)參數(shù)為實(shí)際傳參
System.out.println(result);
} catch (Exception e) {
logger.error("test", e);
}
}
}
運(yùn)行結(jié)果:
E:zhoufysmallpiao-admin
AlTest類 sayHello()方法正在執(zhí)行....
hello word
其中代碼:
int status = javac.run(null, null, null, "-d", System.getProperty("user.dir")+"\target\classes","D:/test/AlTest.java");
把class文件生成到了當(dāng)前工程目錄下的classes目錄(E:zhoufysmallpiao-admin argetclassess)所以classloader是可以加載到的,如果想知道是哪個(gè)類加載器:
Class clz = Class.forName("AlTest");//返回與帶有給定字符串名的類 或接口相關(guān)聯(lián)的 Class 對(duì)象。
Object o = clz.newInstance();
System.out.println(clz.getClassLoader().getSystemClassLoader());
打印的是: sun.misc.Launcher$AppClassLoader@4e0e2f2a 說明使用的是AppClassLoader
當(dāng)然也可以生成到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");
當(dāng)然也可以自定義類加載器,把文件生成在指定的外部目錄 :
public void complierAndRun(){
try {
System.out.println(System.getProperty("user.dir"));
//動(dòng)態(tài)編譯
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
int status = javac.run(null, null, null, "-d", "D:\","D:/test/AlTest.java");
if(status!=0){
System.out.println("沒有編譯成功!");
}
//動(dòng)態(tài)執(zhí)行
//Class clz = Class.forName("AlTest");//返回與帶有給定字符串名的類 或接口相關(guān)聯(lián)的 Class 對(duì)象。
//自定義類加載器的加載路徑
MyClassLoader myClassLoader = new MyClassLoader("D:\");
//包名+類名
Class clz = myClassLoader.loadClass("AlTest");
Object o = clz.newInstance();
Method method = clz.getDeclaredMethod("sayHello");//返回一個(gè) Method 對(duì)象,該對(duì)象反映此 Class 對(duì)象所表示的類或接口的指定已聲明方法
String result= (String)method.invoke(o);//靜態(tài)方法第一個(gè)參數(shù)可為null,第二個(gè)參數(shù)為實(shí)際傳參
System.out.println(result);
} catch (Exception e) {
logger.error("test", e);
}
}
java動(dòng)態(tài)執(zhí)行代碼的代碼, java eval
public class ScriptUtils {
private static final Logger logger = LoggerFactory.getLogger(ScriptUtils.class);
/**
*
* <p>執(zhí)行字符串計(jì)算</p>
* @param express
* @param params
* @return
* @throws ScriptException
*/
@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("表達(dá)式執(zhí)行異常: " + e.getMessage());
}
return result;
}
/**
* 解析字符串, 并將其當(dāng)作表達(dá)式執(zhí)行
* @param express
* @param params
* @return
* @throws ScriptException
*/
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("表達(dá)式執(zhí)行異常: " + e.getMessage());
}
return result;
}
到此這篇關(guān)于Java實(shí)現(xiàn)動(dòng)態(tài)編譯并動(dòng)態(tài)加載的文章就介紹到這了,想要了解更多相關(guān)java動(dòng)態(tài)編譯和動(dòng)態(tài)加載內(nèi)容請(qǐng)搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持!