Rhino print function
Asked Answered
C

4

14

I'm using Rhino 1.7R4 and env.js 1.2 to run Javascript code inside Java

I want to print from my Javascript code a string to the Java console.

According to: http://evilroundabout.blogspot.com.au/2009/11/javascript-printing-rhino.html

I should use: print("Hello world");

but when I do I get:

org.mozilla.javascript.EcmaError: ReferenceError: "print" is not defined. (svg-renderer-highcharts-2.1.4.js#20)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3750)
at org.mozilla.javascript.ScriptRuntime.nameOrFunction(ScriptRuntime.java:1794)
at org.mozilla.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2188)
at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1308)
at script.renderSVGFromObject(svg-renderer-highcharts-2.1.4.js:20)

If I use document.write I don't see any output.

Clarkclarke answered 13/9, 2012 at 4:32 Comment(1)
Bring your javascript and java code.Hepler
P
23

I don't think that will work in embedded mode, I think that will only work in the Rhino console.

You can use java.lang.system.out.println. This should work:-

java.lang.System.out.println("HELLO")
Professoriate answered 13/9, 2012 at 4:47 Comment(0)
G
15

You can use the same scope that the rhino shell uses quite easily. The rhino shell relies on a specially constructed scope instance called Global which defines several functions like "print". The sample below demonstrates how to use Global and the "print" function. This will print "Hello World!" twice to stdout.

import org.mozilla.javascript.Context;
import org.mozilla.javascript.tools.shell.Global;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );

        Context cx = Context.enter();
        Global global = new Global(cx);
        cx.evaluateString(global, "print('Hello World!')", 
                "helloWorld.js", 1, null);
        Context.exit();
    }
}

I discovered this through experimentation after digging through the Rhino shell executable.

And for the sake of completeness here are the other global functions defined by Global:

"defineClass",
"deserialize",
"doctest",
"gc",
"help",
"load",
"loadClass",
"print",
"quit",
"readFile",
"readUrl",
"runCommand",
"seal",
"serialize",
"spawn",
"sync",
"toint32",
"version"
Gatling answered 5/12, 2012 at 15:22 Comment(0)
A
7

You can create your own:

function print() {
    for( var i = 0; i < arguments.length; i++ ) {
       var value = arguments[i];
       java.lang.System.out.print( value );
    }
    java.lang.System.out.println();
}

function printf( format ) {
    java.lang.System.out.printf( format, Array.prototype.slice.call(arguments) );
}
Aleta answered 13/9, 2012 at 4:46 Comment(0)
H
5

as of january, 2014, the list of methods and properties on

new org.mozilla.javascript.tools.shell.Global( org.mozilla.javascript.Context.enter() )

would appear to be the following:

defineClass
deserialize
doctest
gc
getConsole
getErr
getIn
getOut
getPrompts
help
init
init
initQuitAction
installRequire
isInitialized
load
loadClass
pipe
print
quit
readFile
readUrl
runCommand
runDoctest
seal
serialize
setErr
setIn
setOut
setSealedStdLib
spawn
sync
toint32
version
Hanford answered 2/1, 2014 at 19:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.