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.
<script>
tags in the browser. You might find it easier to simply write your own import service in Java and then provide bindings for your JavaScript environment; that's what I've always done (though never with Rhino APIs directly; I've always used the JSEScriptEngine
facilities). – Prairial