问题描述
我需要帮助让CommonJS使用Java 7和Rhino 1.7R3。
I need help getting CommonJS working on Java 7 and Rhino 1.7R3.
Rhino 1.7R3支持CommonJS模块:
Rhino 1.7R3 supports CommonJS modules:
- https://developer.mozilla.org/En/New_in_Rhino_1.7R3
Java 7与Rhino 1.7R3捆绑在一起。不幸的是,Java 7的Rhino是一个修改版本,它不包括 org.mozilla.javascript.commonjs
包:
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:
- http://jdk7.java.net/rhino/README.TXT
我想通过 javax使用Rhino 1.7R3对CommonJS的支持.script
API如下:
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");
(我通过 ScriptEngineManager
验证了我我确实使用的是Rhino 1.7R3引擎。)我想也许我可以将以下依赖项添加到类路径中
(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>
和CommonJS—具体来说, require()
—会开始工作。但事实并非如此。当我尝试使用 require()
我得到
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)
如何让Java 7与完整版Rhino 1.7R3一起使用,以便获得CommonJS支持?
How do I get Java 7 to work with the full version of Rhino 1.7R3 so I can get CommonJS support?
编辑:我发现了以下问题,该问题涉及完全相同的主题:
I found the following question, which deals with exactly the same topic:
受访者表示也许可以用CommonJS Rhino 1.7R3替换有限的Rhino 1.7R3,但没有说明如何做到这一点。这就是我在这里问的问题。
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.
推荐答案
编辑:好像你不需要毕竟使用JVM引导程序类加载器。 Sun已将默认的Rhino实现类放入
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.*
包。但是你加载的Rhino实现将占用
package. But your loaded Rhino implementation will occupy
org.mozilla.javascript.*
因此他们不应该发生碰撞。但是如果出现问题,您可以在bootstrap类加载器的帮助下覆盖JDK中的类。你有两个选择:
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:
基本上你需要覆盖类路径,这样你的Rhino类就会偏好而不是内置类。
Basically you need to override the classpath so that your Rhino classes would take preference instead of the build-in ones.
- 只需将rhino-1.7R3.jar放入-JRE-path \lib \ _ext。这样,Rhino jar将被添加到Java Bootsrap类加载器中,并将在内置JavaScript jar之前加载。
-
或者,如果您无权访问.. / lib / ext你可以使用命令行选项:
- 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.
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
Rhino本身不实现Java Scripting API。为了将Rhino集成到JDK中,Sun已经实现了自己的 ScriptEngine
和 ScriptEngineFactory
。因此,如果您加载自己的 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");
相反,您有两种选择。
-
实施您自己的
ScriptEngine
,ScriptEngineFactory
和Rhino API之上的其他相关类。
了解。但请注意,JDK源代码是GPL 2许可证,因此您应该为Rhino发布自定义脚本引擎包装器,或者仅将这些类用作参考,不要复制代码。
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.
直接使用Rhino API 。 我强烈推荐这种方法。 但基本API相对简单:
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.
- 看看。它是一个JavaScript预处理器,它将您的源代码组合成一个包。
- 重写您的模块,以便它们使用或,然后将它们与。
- Take a look at Browserify. It's a JavaScript preprocessor which will combine your source code into a single bundle.
- Rewrite your modules so that they use either AMD or UMD and then combine them with r.js tool.
这两个选项都要求你添加一个js预处理步骤到你的构建过程,可能会使你的代码调试有点困难。
Both options require you to add a js preprocessing step to your build process and may make debugging your code a bit difficult.
这篇关于Java 7 + Rhino 1.7R3支持CommonJS模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!