Nosairat
Nosairat

Reputation: 502

Error in (Require) with J2V8

I'm trying to use Ripple-lib to get client's balance, inside my java application.

To call Ripple javascript API, I use j2v8 as following:

    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();

but with exception:

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

Could anyone please help me?

Upvotes: 0

Views: 654

Answers (1)

Gopi S
Gopi S

Reputation: 643

If you are running j2v8, the engine knows only pure js, not even window variable, because window, document, and Dom manipulation api's are provided by browser not just engine. Require is not js function. It won't be recognized by v8 engine

Upvotes: 1

Related Questions