有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    试试这个:

    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("JavaScript");
    ScriptContext context = engine.getContext();
    context.setAttribute("jsString", "I am Java String", ScriptContext.ENGINE_SCOPE);
    context.setAttribute("jsBoolean", true, ScriptContext.ENGINE_SCOPE);
    context.setAttribute("jsNumber",  123456, ScriptContext.ENGINE_SCOPE);
    engine.eval("function getValueAndType(obj){return obj + ' - ' + typeof obj;}");
    Assert.assertEquals("Something wen wrong", "I am Java String - string", engine.eval("getValueAndType(jsString)"));
    Assert.assertEquals("Something wen wrong", "true - boolean", engine.eval("getValueAndType(jsBoolean)"));
    Assert.assertEquals("Something wen wrong", "123456 - number", engine.eval("getValueAndType(jsNumber)"));