Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:51:41+00:00 2026-06-05T18:51:41+00:00

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

  • 0

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

Rhino 1.7R3 supports CommonJS modules:

  • https://developer.mozilla.org/En/New_in_Rhino_1.7R3

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:51:43+00:00Added an answer on June 5, 2026 at 6:51 pm

    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, ScriptEngineFactoryand 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help getting my java program ready for release. I have a program
I need help getting setup on Seam 3 modules. When I try to use
I'll try this again. I need help in getting a third option working using
Need a little help getting the following rule working: RewriteCond %{HTTP_HOST} ^asset.*.domainone\.com [NC,AND] RewriteCond
Multi-Test TestNG.xml file results in java.lang.NullPointerException Hello Quality Team, I need help getting TestNG
I am using the package org.json package: I need help with getting the corect
Need help getting Ember-Data working with Zend Rest. At first, I'm familiar with Zend
I need help getting git extensions to run with msysgit. I have had bad
I need help getting a Grease Monkey with JQuery Script to run on a
I need help getting two pieces of data, and comparing them. What I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.