title: Java 7 + Rhino 1.7R3 support for CommonJS modules?
body: <p>I need help getting CommonJS working on Java 7 and Rhino 1.7R3.</p>
  
  <p>Rhino 1.7R3 supports CommonJS modules:</p>
  
  <ul>
  <li><a href="https://developer.mozilla.org/En/New_in_Rhino_1.7R3" rel="nofollow noreferrer">https://developer.mozilla.org/En/New_in_Rhino_1.7R3</a></li>
  </ul>
  
  <p>And Java 7 comes bundled with Rhino 1.7R3. Unfortunately, Java 7's Rhino is a modified version, and it does not include the <code>org.mozilla.javascript.commonjs</code> package:</p>
  
  <ul>
  <li><a href="http://jdk7.java.net/rhino/README.TXT" rel="nofollow noreferrer">http://jdk7.java.net/rhino/README.TXT</a></li>
  </ul>
  
  <p>I would like to use Rhino 1.7R3's support for CommonJS through the <code>javax.script</code> API as follows:</p>
  
  <pre><code>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");
  </code></pre>
  
  <p>(I verified through the <code>ScriptEngineManager</code> that I am indeed using the Rhino 1.7R3 engine.) I thought that perhaps I could just add the following dependency to the classpath</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.mozilla&lt;/groupId&gt;
      &lt;artifactId&gt;rhino&lt;/artifactId&gt;
      &lt;version&gt;1.7R3&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>and CommonJS&mdash;specifically, <code>require()</code>&mdash;would start working. But it doesn't. When I try to use <code>require()</code> I get</p>
  
  <pre><code>Caused by: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "require" is not defined. (&lt;Unknown source&gt;#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)
  </code></pre>
  
  <p>How do I get Java 7 to work with the full version of Rhino 1.7R3 so I can get CommonJS support?</p>
  
  <p><strong>EDIT:</strong> I found the following question, which deals with exactly the same topic:</p>
  
  <p><a href="https://stackoverflow.com/questions/10723180/sanity-check-rhino-does-not-have-a-require-function-right">Sanity check: Rhino does not have a require function, right?</a></p>
  
  <p>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.</p>