Trying to use Rhino, getEngineByName("JavaScript") returns null in OpenJDK 7

Posted by Yuval on Stack Overflow See other posts from Stack Overflow or by Yuval
Published on 2012-04-07T11:56:40Z Indexed on 2012/04/07 17:30 UTC
Read the original article Hit count: 324

Filed under:
|
|
|
|

When I run the following piece of code, the engine variable is set to null when I'm using OepnJDK 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. I noticed OpenJDK 7 has a different directory structure. Perhaps something is not installed right?

$ locate rhino.jar
/usr/lib/jvm/java-6-openjdk/jre/lib/rhino.jar
/usr/lib/jvm/java-7-openjdk-common/jre/lib/rhino.jar
/usr/lib/jvm/java-7-openjdk-i386/jre/lib/rhino.jar

Edit Since ScriptEngineManager uses a ServiceProvider to find the available script engines, I snooped around resources.jar's META-INF/services. I noticed that in OpenJDK 6, resources.jar has a META-INF/services/javax.script.ScriptEngineFactory entry which is missing from OpenJDK 7. Any idea why? I suspect this is a bug?

Here is the contents of that entry (from OpenJDK 6):

#script engines supported

com.sun.script.javascript.RhinoScriptEngineFactory #javascript

Another edit Apparently, according to this thread, the code simply isn't there, perhaps because of merging issues between Sun and Mozilla code. I still don't understand why it was present in OpenJDK 6 and not 7. The class com.sun.script.javascript.RhinoScriptEngineFactory exists in 6's rt.jar but not in 7's. If it was not meant to be included, why is there a OpenJDK 7 rhino.jar then; and why is the source still in the OpenJDK source tree (here)?

© Stack Overflow or respective owner

Related posts about java

Related posts about JavaScript