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

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

Unable to run the javascript that using CryptoJS library in JAVA

问题

  1. public static void runDisplay() {
  2. ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
  3. try {
  4. engine.eval(new FileReader("./resources/JsFunction.js"));
  5. Invocable invocable = (Invocable) engine;
  6. Object result;
  7. result = invocable.invokeFunction("encrypterId", "827AE1001sdsj213jasu721kkao@1sa");
  8. System.out.println(result);
  9. } catch (FileNotFoundException | NoSuchMethodException | ScriptException e) {
  10. e.printStackTrace();
  11. }
  12. }

异常:

  1. javax.script.ScriptException: TypeError: 无法从 crypto-js-3.1.9/crypto-js.js 加载脚本,位于 <eval> 的第 1
  2. at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:469)
  3. at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:453)
  4. at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:405)
  5. at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
  6. 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

  1. load(&#39;crypto-js-3.1.9/crypto-js.js&#39;);
  2. var encrypterId = function(name) {
  3. var context_data = {&quot;referralId&quot;: name};
  4. var secret = CryptoJS.enc.Utf8.parse(JSON.stringify(context_data))
  5. var encoded_referral_id = CryptoJS.enc.Base64.stringify(secret);
  6. return encoded_referral_id;
  7. }

JavaCode:

  1. public static void runDisplay() {
  2. ScriptEngine engine = new ScriptEngineManager().getEngineByName(&quot;nashorn&quot;);
  3. try {
  4. engine.eval(new FileReader(&quot;./resources/JsFunction.js&quot;));
  5. Invocable invocable = (Invocable) engine;
  6. Object result;
  7. result = invocable.invokeFunction(&quot;encrypterId&quot;, &quot;827AE1001sdsj213jasu721kkao@1sa&quot;);
  8. System.out.println(result);
  9. } catch (FileNotFoundException | NoSuchMethodException | ScriptException e) {
  10. e.printStackTrace();
  11. }
  12. }

Exception:

  1. javax.script.ScriptException: TypeError: Cannot load script from crypto-js-3.1.9/crypto-js.js in &lt;eval&gt; at line number 1
  2. at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:469)
  3. at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:453)
  4. at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:405)
  5. at jdk.scripting.nashorn/jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
  6. 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:

确定