3

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?

Blub
  • 13,014
  • 18
  • 75
  • 102
  • You need to "load" Rhino engine using Java Scripting API in your Java application before you can execute Javascript normally (as in the example above) under Java environment. – eee Apr 19 '11 at 13:05
  • You cna only run `.class` files not `.java` as it not a scripting language. If you want to run a methdo on the server you need to pass a request to the server. There must be a javascript library which makes this easier for you than having to do you own http calls, but I don't know what it is. – Peter Lawrey Apr 19 '11 at 13:06
  • 1
    Running a JavaScript script with Java references won't work without Rhino as the script engine. Either you need: (1) to compile your Javascript script using Rhino compiler to become a Java .class file that can be executed using java.exe; or (2) to run the script through Rhino shell interpreter; or (3) to load Rhino engine using Java scripting support under Java app and execute `eval()` upon the script. – eee Apr 19 '11 at 13:18
  • @eee @Peter: I'll edit my question because there is too little room here in the comments box. – Blub Apr 19 '11 at 13:32
  • 1
    After reading your edited post: yes, it is exactly the case. It is the same if you try `Jython`, the Python engine for Java. – eee Apr 19 '11 at 13:39

1 Answers1

3

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.

FabienB
  • 1,104
  • 2
  • 11
  • 22