本例演示怎样通过JS脚本导入JAVA类包
我们创建JS引擎后
通过eval方法调用 getScript()
JS脚本中importPackage(java
util)为导入包
package dejavase;
import javaxscriptScriptEngineManager;
import javaxscriptScriptEngine;
import javaxscriptScriptException;
public class AjavaImportPackageExample {
public static void main(String[] args) {
ScriptEngineManager manager = new ScriptEngineManager()
ScriptEngine engine = managergetEngineByExtension(js)
try {
engineeval(getScript())
} catch (ScriptException e) {
eprintStackTrace()
}
}
private static String getScript() {
StringBuilder sb = new StringBuilder()
sbappend(importPackage(javautil))
sbappend()
sbappend(var today = new Date())
sbappend(println(原创日期 + today))
return sbtoString()
}
}
运行结果
原创日期 Tue Mar :: GMT+ (CST)