简体   繁体   中英

Java 7 + Rhino 1.7R3 support for CommonJS modules?

I need help getting CommonJS working on Java 7 and Rhino 1.7R3.

Rhino 1.7R3 supports CommonJS modules:

And Java 7 comes bundled with Rhino 1.7R3. Unfortunately, Java 7's Rhino is a modified version, and it does not include the org.mozilla.javascript.commonjs package:

I would like to use Rhino 1.7R3's support for CommonJS through the javax.script API as follows:

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
engine.put("markdown", markdown);
engine.eval("var html = require('./Markdown.Sanitizer').getSanitizingConverter().makeHtml(markdown);");
return (String) engine.get("html");

(I verified through the ScriptEngineManager that I am indeed using the Rhino 1.7R3 engine.) I thought that perhaps I could just add the following dependency to the classpath

<dependency>
    <groupId>org.mozilla</groupId>
    <artifactId>rhino</artifactId>
    <version>1.7R3</version>
</dependency>

and CommonJS—specifically, require() —would start working. But it doesn't. When I try to use require() I get

Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "require" is not defined. (<Unknown source>#2)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3773)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.constructError(ScriptRuntime.java:3751)
    at sun.org.mozilla.javascript.internal.ScriptRuntime.notFoundError(ScriptRuntime.java:3836)

How do I get Java 7 to work with the full version of Rhino 1.7R3 so I can get CommonJS support?

EDIT: I found the following question, which deals with exactly the same topic:

Sanity check: Rhino does not have a require function, right?

The respondent suggests that maybe it's possible to replace the limited Rhino 1.7R3 with the CommonJS Rhino 1.7R3, but doesn't say how one might do that. That's what I'm asking about here.

Edit : Seems like you don't need to use JVM bootstrap classloader after all. Sun has put the default Rhino implementation classes into

sun.org.mozilla.javascript.*

package. But your loaded Rhino implementation will occupy

org.mozilla.javascript.*

Thus they shouldn't collide. However if something goes wrong you can override classes in JDK with the help of bootstrap classloader. You have two options:

Basically you need to override the classpath so that your Rhino classes would take preference instead of the build-in ones.

  1. Simply put the rhino-1.7R3.jar to your-JRE-path\\lib\\ext. This way Rhino jar will be added to Java Bootsrap classloader and will be loaded before the build-in JavaScript jar.
  2. Alternatively, if you don't have an access to ../lib/ext you can use a command-line option:

     -Xbootclasspath/a:path/to/rhino-1.7R3.jar 

The Rhino itself does not implement Java Scripting API. In order to integrate Rhino into JDK Sun had implemented their own ScriptEngine and ScriptEngineFactory . Thus, if you load your own rhino-1.7R3.jar you won't be able to use Common JS if you load your scripts with

ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");

Instead you have two options.

  1. Implement your own ScriptEngine , ScriptEngineFactory and other related classes on top of Rhino API. See how Oracle does it . Note however, that JDK sources are under GPL 2 license so you should either release your custom Script Engine wrapper for Rhino or only use those classes as a reference and don't copy the code.

  2. Use Rhino API directly . I highly recommend this approach. There are docs and examples on Mozilla website but the basic API is relatively simple:

     // Execution environment for Rhino // there should be only one context in a giver thread Context cx = Context.enter(); // Object.prototype, Function prototype, etc. Scriptable scope = cx.initStandardObjects(); // Execute script from a given java.io.Reader Object result = cx.evaluateReader(scope, reader, 0, null); // If returning result isn't sufficient for your needs // you can do something like this: Object someVar = scope.get("someVar"); // Don't forget to close the context when you're done Context.exit(); 

Alternatively, I can give you a number of JS-only solutions.

  1. Take a look at Browserify . It's a JavaScript preprocessor which will combine your source code into a single bundle.
  2. Rewrite your modules so that they use either AMD or UMD and then combine them with r.js tool .

Both options require you to add a js preprocessing step to your build process and may make debugging your code a bit difficult.

This question is old, but for those coming here from Google, you can setup CommonJS support in Rhino with the following (below is just one way):

(I am just calling Rhino directly, not using Java's Scripting API)

List<URI> paths = Arrays.asList(new URI[] { new File("js/require/paths/here").toURI() });
Context ctx = Context.enter();
Scriptable scope = ctx.initStandardObjects();

new RequireBuilder()
    .setModuleScriptProvider(new SoftCachingModuleScriptProvider(
        new UrlModuleSourceProvider(paths, null)))
    .setSandboxed(true)
    .createRequire(ctx, scope)
    .install(scope);

//
// now you can ctx.evaluateString(...) and use require()
//

Context.exit();

Now you may have two files:

main.js

require('require-test.js')()

require-test.js

module.exports = function() {
    java.lang.System.out.println('The CommonJS require function works!')
}

If you use Gradle build system. I successfully fixed this issue via adding this dependency in the project gradle file. (based on here )

compile group: 'org.mozilla', name: 'rhino', version: '1.7.10'

And you have to replace imported package name from sun.org.mozilla.javascript.internal to org.mozilla.javascript in your java class file.

// import sun.org.mozilla.javascript.internal.NativeObject; // <=== Do not use this package.
import org.mozilla.javascript.NativeObject;

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

And everything works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM