无法在JAVA中运行使用CryptoJS库的javascript代码。

huangapple go评论65阅读模式
英文:

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(&#39;crypto-js-3.1.9/crypto-js.js&#39;);

var encrypterId = function(name) {

    var context_data = {&quot;referralId&quot;: 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(&quot;nashorn&quot;);
        try {
            engine.eval(new FileReader(&quot;./resources/JsFunction.js&quot;));
            Invocable invocable = (Invocable) engine;
            Object result;
            result = invocable.invokeFunction(&quot;encrypterId&quot;, &quot;827AE1001sdsj213jasu721kkao@1sa&quot;);
            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 &lt;eval&gt; 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(&quot;./resources/JsFunction.js&quot;)); 之前的那一行插入 engine.eval(new FileReader(&quot;./resources/crypto-js-3.1.9/crypto-js.js&quot;));

我相信这样可以将 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(&quot;./resources/JsFunction.js&quot;)); just insert a line engine.eval(new FileReader(&quot;./resources/crypto-js-3.1.9/crypto-js.js&quot;));

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.

huangapple
  • 本文由 发表于 2020年9月17日 19:34:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63937163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定