aaa
package cn.sniper.spider.utils;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.junit.Test;
public class Java4JsUtil {
@Test
public void testJs1() throws Exception {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByExtension("js");
StringReader reader = new StringReader("function getNum(num){return num}");
engine.eval(reader);
Invocable invocable = (Invocable)engine;
Object result = invocable.invokeFunction("getNum", "2");
System.out.println(result);
}
@Test
public void testJs2() throws Exception {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByExtension("js");
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\test.js")));
engine.eval(reader);
Invocable invocable = (Invocable)engine;
Object result = invocable.invokeFunction("getNum", "2");
System.out.println(result);
}
@Test
public void testOnlineJs() throws Exception {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByExtension("js");
URL url2 = new URL("http://10.161.56.231:8888/tjfxpt/resources/js/common/common.js");
InputStream inputStream = url2.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
System.out.println(reader.readLine());
engine.eval(reader);
Invocable invocable = (Invocable)engine;
Object result = invocable.invokeFunction("fmoney", new Integer[]{10, 2});
System.out.println(result);
}
}