英文:
Unable to run the javascript that using CryptoJS library in JAVA
问题
public static void runDisplay() {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
try {
engine.eval(new FileReader("./resources/JsFunction.js"));
Invocable invocable = (Invocable) engine;
Object result;
result = invocable.invokeFunction("encrypterId", "827AE1001sdsj213jasu721kkao@1sa");
System.out.println(result);
} catch (FileNotFoundException | NoSuchMethodException | ScriptException e) {
e.printStackTrace();
}
}
异常:
javax.script.ScriptException: TypeError: 无法从 crypto-js-3.1.9/crypto-js.js 加载脚本,位于 <eval> 的第 1 行
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:469)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:453)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:405)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:149)
有人可以帮我运行这个JS代码并返回值吗?或者是否可能编写等效的Java代码?
英文:
I'm trying to run the JS function in Java code and the JS function is not executing since it had some third party library that needs to be load.
JsFunction.js
load('crypto-js-3.1.9/crypto-js.js');
var encrypterId = function(name) {
var context_data = {"referralId": name};
var secret = CryptoJS.enc.Utf8.parse(JSON.stringify(context_data))
var encoded_referral_id = CryptoJS.enc.Base64.stringify(secret);
return encoded_referral_id;
}
JavaCode:
public static void runDisplay() {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
try {
engine.eval(new FileReader("./resources/JsFunction.js"));
Invocable invocable = (Invocable) engine;
Object result;
result = invocable.invokeFunction("encrypterId", "827AE1001sdsj213jasu721kkao@1sa");
System.out.println(result);
} catch (FileNotFoundException | NoSuchMethodException | ScriptException e) {
e.printStackTrace();
}
}
Exception:
javax.script.ScriptException: TypeError: Cannot load script from crypto-js-3.1.9/crypto-js.js in <eval> at line number 1
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:469)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:453)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:405)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:149)
Can someone help me to run this JS and return the value ? else is it possible to write the equivalent code in Java?
答案1
得分: 0
我认为你可以这样做:
从 JsFunction.js 中移除 'load' 行。
在你的 Java 代码中,在 engine.eval(new FileReader("./resources/JsFunction.js"));
之前的那一行插入 engine.eval(new FileReader("./resources/crypto-js-3.1.9/crypto-js.js"));
。
我相信这样可以将 crypto-js.js 文件的内容放入 ScriptEngine 的作用域中,随后的 JsFunction.js 调用应该会起作用。
我曾经使用更简单的 JS 文件进行过类似的测试。
英文:
I think you can do it this way:
Remove the 'load' line from JsFunction.js
In your JavaCode, in the line immediately before engine.eval(new FileReader("./resources/JsFunction.js"));
just insert a line engine.eval(new FileReader("./resources/crypto-js-3.1.9/crypto-js.js"));
I believe that puts the contents of the crypto-js.js file into scope in the ScriptEngine, and the subsequent JsFunction.js call should work.
I did something similar with much simpler JS files as a test case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论