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 8092429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:16:53+00:00 2026-06-05T20:16:53+00:00

I’m trying to get require.js to load modules on the server-side with Java 6

  • 0

I’m trying to get require.js to load modules on the server-side with Java 6 and Rhino.

I’m able to load require.js itself just fine. Rhino can see the require() function. I can tell because Rhino complains that it can’t find the function when I change require() to something else like requireffdkj().

But when I try to require even a simple JS, like hello.js

var hello = 'hello';

using either of the following:

require('hello');
require('./hello');

it doesn’t work. I get

Caused by: javax.script.ScriptException: sun.org.mozilla.javascript.internal.JavaScriptException: [object Error] (<Unknown source>#31) in <Unknown source> at line number 31
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:153)
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:167)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)

I have my hello.js at the top of the Java classpath. That’s where I have require.js as well. I tried moving hello.js everywhere I could think it might possibly go, including the root of my hard drive, the root of my user directory, the directory from which I’m running my Java app, etc. Nothing works.

I looked at the CommonJS spec (http://wiki.commonjs.org/wiki/Modules/1.0) and it says that top-level IDs (like hello) are resolved from the “conceptual module name space root”, whereas relative IDs (like ./hello) are resolved against the calling module. I’m not sure where either of those baselines is, and I suspect that’s the issue.

Any suggestions? Can I even use require.js from Rhino?

EDIT: Thinking that I need to set the environment up as per Pointy’s suggestion in the comment below, I tried evaluating r.js as well. (I tried evaluating after evaluating require.js, and then again before require.js.) In either case I get an error:

Caused by: javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "arguments" is not defined. (<Unknown source>#19) in <Unknown source> at line number 19
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:153)
    at com.sun.script.javascript.RhinoScriptEngine.eval(RhinoScriptEngine.java:167)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:247)

“arguments” appears to be a variable in r.js. I think it’s for command line arguments, so I don’t think r.js is the right path for what I’m trying to do. Not sure though.

  • 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-05T20:16:55+00:00Added an answer on June 5, 2026 at 8:16 pm

    require.js works well with rhino. Recently, I used it in a project.

    1. You have to make sure to use r.js (not require.js) , modified version of require.js for rhino.
    2. You have to extend ScritableObject class to implement load and print function. When you call require(["a"]), the load function in this class will be called, you can tweak this function to load the js file from any location. In the below example, I load from classpath.
    3. You have to define the property arguments in the sharedscope as shown below in the sample code
    4. Optionally, you can configure the sub path using require.config, to specify the subdirectory inside classpath where js files are located.

    JsRuntimeSupport

    public class JsRuntimeSupport extends ScriptableObject {
    
        private static final long serialVersionUID = 1L;
        private static Logger logger = Logger.getLogger(JsRuntimeSupport.class);
        private static final boolean silent = false;
    
        @Override
        public String getClassName() {
            return "test";
        }
    
        public static void print(Context cx, Scriptable thisObj, Object[] args,
                Function funObj) {
          if (silent)
            return;
            for (int i = 0; i < args.length; i++)
              logger.info(Context.toString(args[i]));
        }
    
        public static void load(Context cx, Scriptable thisObj, Object[] args,
                Function funObj) throws FileNotFoundException, IOException {
            JsRuntimeSupport shell = (JsRuntimeSupport) getTopLevelScope(thisObj);
            for (int i = 0; i < args.length; i++) {
                logger.info("Loading file " + Context.toString(args[i]));
                shell.processSource(cx, Context.toString(args[i]));
            }
        }
    
        private void processSource(Context cx, String filename)
                throws FileNotFoundException, IOException {
            cx.evaluateReader(this, new InputStreamReader(getInputStream(filename)), filename, 1, null);
        }
    
        private InputStream getInputStream(String file) throws IOException {
            return new ClassPathResource(file).getInputStream();
        }
    }
    

    Sample Code

    public class RJsDemo {
    
        @Test
        public void simpleRhinoTest() throws FileNotFoundException, IOException {
        Context cx = Context.enter();
    
        final JsRuntimeSupport browserSupport = new JsRuntimeSupport();
    
        final ScriptableObject sharedScope = cx.initStandardObjects(browserSupport, true);
    
        String[] names = { "print", "load" };
        sharedScope.defineFunctionProperties(names, sharedScope.getClass(), ScriptableObject.DONTENUM);
    
        Scriptable argsObj = cx.newArray(sharedScope, new Object[] {});
        sharedScope.defineProperty("arguments", argsObj, ScriptableObject.DONTENUM);
    
        cx.evaluateReader(sharedScope, new FileReader("./r.js"), "require", 1, null);
        cx.evaluateReader(sharedScope, new FileReader("./loader.js"), "loader", 1, null);
    
        Context.exit();
    
      }
    
    }
    

    loader.js

    require.config({
        baseUrl: "js/app"
    });
    
    require (["a", "b"], function(a,  b) {
        print('modules loaded');
    });
    

    js/app directory should be in your classpath.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string

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.