使用J2V8时出现(需要)错误

时间:2018-03-15 14:51:04

标签: javascript java node.js j2v8

我正在尝试使用Ripple-lib在我的java应用程序中获得客户端的平衡。

要调用Ripple javascript API,我使用j2v8如下:

    V8 runtime = V8.createV8Runtime();
    runtime.executeVoidScript(""
            + "const RippleAPI = require('ripple-lib').RippleAPI;\n"
            + "const api = new RippleAPI({\n"
            + "    server: 'wss://s.altnet.rippletest.net:51233'\n"
            + "});\n"
            + "var client = {\n"
            + "    getBalance: function (address) {\n"
            + "        api.connect().then(() => {\n"
            + "            api.getBalances(address).then(balances => {\n"
            + "                return JSON.stringify(balances, null, 2);\n"
            + "            });\n"
            + "        })\n"
            + "    }\n"
            + "};");
    V8Object client = runtime.getObject("client");
    V8Array parameters = new V8Array(runtime);
    parameters.push("rHY6yUsQaEigs867XUgaMp89Hhm2eJs5jQ");
    String result = client.executeStringFunction("getBalance", parameters);
    System.out.println(result);
    parameters.release();
    runtime.release();

但有例外:

Exception in thread "main" undefined:1: ReferenceError: require is not defined
const RippleAPI = require('ripple-lib').RippleAPI;
                  ^
ReferenceError: require is not defined
    at <anonymous>:1:19
com.eclipsesource.v8.V8ScriptExecutionException

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

如果你正在运行j2v8,引擎只知道pre js,甚至不知道window变量,因为window,document和Dom操作api是由浏览器而不仅仅是引擎提供的。要求不是js功能。它不会被v8引擎识别