javascript 犀牛打印功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12399462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:06:05  来源:igfitidea点击:

Rhino print function

javajavascriptdebuggingscriptingrhino

提问by Greg Pagendam-Turner

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

我正在使用 Rhino 1.7R4 和 env.js 1.2 在 Java 中运行 Javascript 代码

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

我想从我的 Javascript 代码打印一个字符串到 Java 控制台。

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

根据:http: //evilroundabout.blogspot.com.au/2009/11/javascript-printing-rhino.html

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

我应该使用: 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.

如果我使用 document.write 我看不到任何输出。

回答by user1545858

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

我认为这不会在嵌入式模式下工作,我认为这只会在 Rhino 控制台中工作。

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

您可以使用 java.lang.system.out.println。这应该有效:-

java.lang.System.out.println("HELLO")

回答by nlloyd

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.

您可以很容易地使用与 rhino shell 相同的作用域。rhino shell 依赖于一个特殊构造的名为 Global 的作用域实例,它定义了几个函数,如“print”。下面的示例演示了如何使用 Global 和“打印”功能。这将打印“Hello World!” 两次到标准输出。

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.

在深入研究Rhino shell 可执行文件后,我通过实验发现了这一点。

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

为了完整起见,这里是Global定义的其他全局函数:

"defineClass",
"deserialize",
"doctest",
"gc",
"help",
"load",
"loadClass",
"print",
"quit",
"readFile",
"readUrl",
"runCommand",
"seal",
"serialize",
"spawn",
"sync",
"toint32",
"version"

回答by chubbsondubs

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

回答by flow

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

截至 2014 年 1 月,方法和属性列表

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