Get Rhino JS to see Java class

孤街醉人 提交于 2019-12-12 09:44:55

问题


I'm playing with Rhino, and I've had success using Java classes from the stdlib, but not from Java code I compiled here.

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:

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.

Any way I do it, from in the interpreter I see:

js> java.lang.System.getProperty("java.class.path")
/Users/me/blah/rhino.jar

回答1:


You need to run Rhino like this:

java -cp /path/to/rhino/js.jar:. org.mozilla.javascript.tools.shell.Main

This adds the current directory to the classpath. Using -jar clobbers the classpath. (The classpath separator depends on your OS.)

Then try

js> Packages.NanoHTTPD
[JavaClass NanoHTTPD]

If it says [JavaPackage NanoHTTPD], it means it hasn't found a class by that name.

You can't instantiate NanoHTTPD anyways, so I'm guessing you want to try Packages.NanoHTTPD.main([]) or something.




回答2:


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:

less `which rhino`



回答3:


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 -jar switch is that you can't define classpath in this case and without setting classpath you can't access to your own packages and classes.
To be able to set classpath you need to run Rhino using -cp switch. In this case you set your classpath by -cp switch 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.myclass placed in mylib.jar If 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()




回答4:


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.



来源:https://stackoverflow.com/questions/4481696/get-rhino-js-to-see-java-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!