1

I am trying to use rhino. I want to use window.atob(param) javascript function from java code. First of all is it possible? This is what I have tried.

ScriptEngine runtime = null;
try {
    runtime = new ScriptEngineManager().getEngineByName("javascript");
    runtime.put(
            "str",
            "PGh0bJvZHk+PC9odG1sPg==");
    System.out.println((String)runtime.eval("window.atob(str)"));

} catch (Exception ex) {
    ex.printStackTrace();
}

I am getting the following exception.

sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "window" is not defined

I know I can decode using java but can any one let me know how to do it using rhino?

3 Answers 3

1

window (and document, while we're at it) are objects that are tied to a web page in a browser. These concepts don't exist within Rhino so you can't access any of the window's methods.

There is a request on github to add support for atob though. Until then you'll have to implement it yourself or find a library that has it.

Sign up to request clarification or add additional context in comments.

Comments

0

As Vache wrote, the window object only exists in browsers. However, you can simulate a browser using env.js.

After loading this script, you get access to the window object.

1 Comment

how can i use it to get window object using env.js
0

Thanks to @feuerball. I have found the solution to my problem after going through SO and Docs.

Here is the code sample:

        import org.mozilla.javascript.Context;
        import org.mozilla.javascript.ContextFactory;
        import org.mozilla.javascript.tools.shell.Global;
        import org.mozilla.javascript.tools.shell.Main;
        .................
        Context cx = ContextFactory.getGlobal().enterContext();
        cx.setOptimizationLevel(-1);
        cx.setLanguageVersion(Context.VERSION_1_5);
        Global global = Main.getGlobal();
        global.init(cx);
        try {
            Main.processSource(cx, "C:\\Desktop\\env.rhino.1.2.js");
            System.out.println(cx.evaluateString(global, "window.atob(\"UmYXNlahcg==\")", "js", 1, null));
        } catch (IOException e) {
            e.printStackTrace();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.