views:

141

answers:

3

I've written some scripts in Javascript under Rhino 1.7, one of them starts a minimal http server and accepts JS commands in input.

Now, if I call (from within Rhino):

engine = ScriptEngineManager().getEngineByName("JavaScript");

I get the builtin JS engine (from Java 1.6), that is an older version of Rhino, and lacks some functions (like JavaAdapter for multiple interfaces).

How do I get the Rhino Engine instead of that? Do I need ScriptEngineManager.getEngineFactories() or what else?

A: 

Before invoking your initial script, why don't you set the engine you're using as a context variable inside the script? That way, inside the script, you'll have access to the engine that is running it.

Kevin
I'm not sure I understand what you mean.However: changing the java code which runs the javascript engine is what I'm trying to avoid (otherwise I'd implement the whole http-server thing in Java).
G B
A: 

What you want to achieve is to select a certain version of an script engine which implements "JavaScript". The correct way to do that is to call ScriptEngineManager.getEngineFactories() and then check the results of getLanguageName() and getEngineVersion().

Aaron Digulla
I already did, but Rhino doesn't register an engine factory.
G B
+1  A: 

I found it out myself (trial and error). As noted above, Rhino doesn't register an engine factory. You can get the current engine (as a context and a scriptable object):

cx = Context.getCurrentContext();
scope = new ImporterTopLevel(cx);

With these objects, I can run my scripts or command lines using evalString/evalReader.

G B