Reputation: 13634
I've read through the docs here: Mozilla's 'Scripting Java'
These examples don't answer my question either: Rhino Liveconnect example
How do my javascript files know where to find "java.lang" for example? Of course it works inside the Rhino shell, but I need to run java code from inside my javascript files, not from the shell. I can only see .java files in the downloaded source.
I want to call serverside java methods in my serverside nodejs javascript. Doesn't Rhino somehow need to be started up to provide my javascript with the ability to comprehend java?
Edit: @eee So if I understand correctly, Rhino doesn't actually let me run java from javascript, it just translate all my javascript into java .class files...which I have to execute inside a java file after all? That kind of defeates the purpose of calling java from javascript. The whole idea is to be able to call any java code from inside my javascript code without having to build new java libraries.
Doesn't that mean that I can't use Rhino? Nodejs uses the V8 engine to execute javascript, so I assume that a single javascript file can't be used by both V8 and Rhino...I was hoping that I could call java methods, which would start up Rhino which would translate those calls to Java. Java itself would then return the variables filles with some data.
@Peter: Why http calls? Both my javascript and my java lie on the same server for now. Anyway, what you are saying wouldn't exactly require Rhino, would it? I'd create a bunch of .class files and then call 'java myfile.class -v "firstvar" -x "secondvar"'.. am I understanding you correctly?
Upvotes: 3
Views: 2255
Reputation: 1121
Rhino does actually let me run java from javascript scripts. There is not much to do. LiveConnect gives you access to everything in the java.* package. If you want your own code to be accessible, you just need to add your class (compiled Java code) to the application's classpath.
I want to call serverside java methods in my serverside nodejs javascript.
Rhino is a javascript interpreter, meaning it runs javascript code. V8 is NodeJS' javascript interpreter. You must chose either Rhino or V8. V8 is in C++ and is deeply embedded in NodeJS. Rhino is in Java and can't replace V8 easily. There are projects (long term) to port NodeJS on Rhino, but don't wait for that unless you are ready to get involved.
Upvotes: 3