Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 0 down vote accepted

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.

share|improve this answer
    
how can i use it to get window object using env.js –  BRS Jul 14 at 14:07
    
I did not use it before. Please refer to the docs. –  feuerball Jul 14 at 19:30
add comment

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.

share|improve this answer
add comment

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();
        }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.