14

When I run the following piece of code, the engine variable is set to null when I'm using OpenJDK 7 (java-7-openjdk-i386).

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class TestRhino {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        try {
            System.out.println(engine.eval("1+1"));
        } catch (ScriptException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

It runs fine with java-6-openjdk and Oracle's jre1.7.0. Any idea why?

I'm using Ubuntu 11.10. All JVMs are installed under /usr/lib/jvm.

Prof. Falken
  • 22,327
  • 18
  • 94
  • 163
Yuval
  • 2,607
  • 25
  • 38
  • See also [Javascript engine can not be found](http://stackoverflow.com/questions/6089773); JDK providers can choose what script engines to package, if any. – McDowell Apr 07 '12 at 13:59
  • interesting, jdk 7 has the same problem for me on debian. jrunsript of jdk6 runs the javascript engine and the jdk 7 jrunsript prints "engine for language js cannot be found". – josefx Apr 07 '12 at 19:45
  • Note: You cannot be certain that a given script engine is available. If you _really_ need it, bring it along yourself. – Thorbjørn Ravn Andersen Apr 10 '12 at 08:30
  • [Fixed in 7~u3-2.1.1~pre1-1ubuntu2](https://bugs.launchpad.net/ubuntu/+source/openjdk-7/+bug/982501) – Yuval Jul 28 '12 at 18:38
  • having the same issue on OSX, if anyone could answer _how_ to provide my own service I'll up vote. I've tried just having the rhino jar on the classpath and it's not working – The Trav Dec 03 '13 at 03:31

2 Answers2

3

[Update: This was a bug, which has now been fixed]

In short, this might be a bug.

Rhino support classes (com.sun.script.javascript.*) are not compiled into rt.jar1 (though I found references to this being a merging issue between Sun and Mozilla, and I know there have been namespace issues, they do exist in the OpenJDK 7 source and are referenced in the makefile), and they're missing from resources.jar's META-INF/services as well. This is not the case with OpenJDK 6, which has this as a META-INF/services/javax.script.ScriptEngineFactory entry:

#script engines supported

com.sun.script.javascript.RhinoScriptEngineFactory #javascript

Though this might be a distro decision2 there is no written reason for it, so I filed a bug #982501 in the Ubuntu OpenJDK 7 launchpad. Will give a better answer once I get one.

1 according to this thread,

I had heard somewhere that Mozilla didn't accept Suns changes into their mainline branch for reasons unknown but that was prior to being opensourced.

There have also been some collision issues caused by conflicting versions of JRE and a BYOR (bring-you-own-Rhino) version (e.g. bug #255149 on Ubuntu OpenJDK 7 launchpad). However, the source is in the OpenJDK 7 source and mentioned in the makefile, and BSD port, mentioned in the above thread, has identical sources.

2 According to this Sun bug #6876736, this is to a distro-based decision:

The rhino sources are not part of OpenJDK, it is up to the distros to add it.

Somewhat in contrast to the fact that they're checked in to the OpenJDK mercurial, but I think the idea is that Rhino is not in the JDK spec.

NealeU
  • 1,078
  • 10
  • 20
Yuval
  • 2,607
  • 25
  • 38
0

You must register the Rhino service, but I do not know exactly how, is by creating a file inside META-INF

OR You can skip the Script API and use it directly.

Check this basic example:

https://github.com/mozilla/rhino/blob/master/examples/Control.java

Daniel De León
  • 11,681
  • 5
  • 76
  • 66