javascript 获取 Rhino JS 查看 Java 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4481696/
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
Get Rhino JS to see Java class
提问by Ken
I'm playing with Rhino, and I've had success using Java classes from the stdlib, but not from Java code I compiled here.
我正在使用Rhino,并且我已经成功地使用了来自 stdlib 的 Java 类,但没有使用我在这里编译的 Java 代码。
For example, this works fine:
例如,这工作正常:
print(new java.util.Date());
But with NanoHTTPD(single .java file, no namespace, same folder), I'm having no luck at all:
但是对于NanoHTTPD(单个 .java 文件,没有命名空间,相同的文件夹),我一点运气都没有:
js> new Packages.NanoHTTPD()
js: "<stdin>", line 4: uncaught JavaScript runtime exception: TypeError: [JavaPackage NanoHTTPD] is not a function, it is object.
    at <stdin>:4
I'm sure it's something simple. What am I missing?
我确定这很简单。我错过了什么?
EDIT: I'm launching it like this:
编辑:我是这样启动的:
$ CLASSPATH=. java -jar rhino.jar
or this:
或这个:
$ java -classpath . -jar rhino.jar
Or I moved NanoHTTPD.java into the folder "./nano", added package nano;to the top of the file, compiled it, and then replaced "." with "nano" in the above classpath assignments.
或者我把NanoHTTPD.java移动到“./nano”文件夹中,添加package nano;到文件顶部,编译,然后替换“.” 在上述类路径分配中使用“nano”。
Any way I do it, from in the interpreter I see:
无论我怎么做,从我看到的解释器中:
js> java.lang.System.getProperty("java.class.path")
/Users/me/blah/rhino.jar
回答by erjiang
You need to run Rhino like this:
你需要像这样运行 Rhino:
java -cp /path/to/rhino/js.jar:. org.mozilla.javascript.tools.shell.Main
This adds the current directory to the classpath.  Using -jarclobbers the classpath. (The classpath separator depends on your OS.)
这会将当前目录添加到类路径。使用-jarclobbers 类路径。(类路径分隔符取决于您的操作系统。)
Then try
然后试试
js> Packages.NanoHTTPD
[JavaClass NanoHTTPD]
If it says [JavaPackage NanoHTTPD], it means it hasn't found a class by that name.
如果显示为[JavaPackage NanoHTTPD],则表示尚未找到该名称的类。
You can't instantiate NanoHTTPD anyways, so I'm guessing you want to try Packages.NanoHTTPD.main([])or something.
你无论如何都不能实例化 NanoHTTPD,所以我猜你想尝试Packages.NanoHTTPD.main([])什么。
回答by Amit Yaron
In my Linux, I found that the command 'rhino' is a shell script that runs 'org.mozilla.javascript.shell.Main' with the option '-classpath'. You can edit the file to include the path to your class. 
I think the script is self explanatory.
If you use Linux, type:
在我的 Linux 中,我发现命令“rhino”是一个 shell 脚本,它运行带有“-classpath”选项的“org.mozilla.javascript.shell.Main”。您可以编辑文件以包含类的路径。
我认为脚本是不言自明的。如果您使用 Linux,请键入:
less `which rhino`
回答by Space cowboy
If you don't plan to use your own clases in Rhino usually you run it in following way:java -jar ./js.jar
The problem to use the -jarswitch is that you can't define classpathin this case and without setting classpathyou can't access to your own packages and classes.
To be able to set classpathyou need to run Rhino using -cpswitch. In this case you set your classpathby -cpswitch which shall include package of Rhino and your packages and also you need pass Rhino's main class path inside the package (org.mozilla.javascript.tools.shell.Main)
Here is an example how to add your own packages to Rhino classpath:
Suppose you have your class mypackage.myclassplaced in mylib.jarIf you want to get this class available in your Rhino session you need to run Rhino in following way:java -cp "./js.jar;../mylib.jar" org.mozilla.javascript.tools.shell.Main
Then you can access to your class:jc> mc_obj = new Packages.mypackage.myclass()
如果你不打算在 Rhino 中使用你自己的类,通常你可以通过以下方式运行它:java -jar ./js.jar
使用-jarswitch的问题是classpath在这种情况下你不能定义,如果不设置,classpath你就不能访问自己的包和类。
为了能够进行设置,classpath您需要使用-cpswitch运行 Rhino 。在这种情况下,你设置你的classpathby-cp开关,它应该包括 Rhino 的包和你的包,你还需要在包中传递 Rhino 的主类路径 ( org.mozilla.javascript.tools.shell.Main)
下面是一个如何将你自己的包添加到 Rhino 类路径的示例:
假设你有你的类mypackage.myclass放置在mylib.jar如果你想在你的 Rhino 会话中使用这个类,你需要按以下方式运行 Rhino:java -cp "./js.jar;../mylib.jar" org.mozilla.javascript.tools.shell.Main
然后你可以访问你的类:jc> mc_obj = new Packages.mypackage.myclass()
回答by killdash9
Ensure that the current directory is included in your classpath. The default classpath is the current directory but if the classpath has been set to something else (say by the rhino startup script) then you could run into this.
确保当前目录包含在您的类路径中。默认的类路径是当前目录,但如果类路径已设置为其他内容(例如犀牛启动脚本),那么您可能会遇到此问题。
You might also try placing your test class in a package just to see if it has some quirk with top-level classes.
您也可以尝试将您的测试类放在一个包中,以查看它是否与顶级类有一些怪癖。

