0

我正在尝试在我的 java 应用程序中使用Ripple-lib来获取客户端的余额。

要调用 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

有人可以帮我吗?

4

1 回答 1

0

如果你运行的是 j2v8,引擎只知道 pre js,甚至不知道 window 变量,因为 window、document 和 Dom 操作 api 是由浏览器提供的,而不仅仅是引擎。Require 不是 js 函数。v8引擎无法识别

于 2018-03-15T18:14:11.797 回答