Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to load js libraries in an embedded rhino application?

1,898 views
Skip to first unread message

doles

unread,
Mar 2, 2010, 11:39:15 AM3/2/10
to
Hello,

How does one load a library such as jQuery or prototype into the rhino
context when running as an embedded app within a java program? I am
using Java 6, rhino 1.7R2. I can totally run functions and eval
arbitrary code, but I cant find any documentation or even class files
in the source tree that scream to me "use me to load libraries". So
what class/method in Rhino actually satisfies the load (file)
semantics in the command line?

Thanks for your attention!

Sachin

Quentin ADAM (waxzce) - Kan OP

unread,
Mar 3, 2010, 1:36:58 AM3/3/10
to
Hello,

rhino is "just" an interpreter of javascript, if you want use jquery
or prototype you need a browser environment (DOM, and many of other
things).
a project exist for rhino : env-js http://github.com/thatcher/env-js
This is an javascript implementation of a browser env for rhino.

have fun

Quentin

Attila Szegedi

unread,
Mar 3, 2010, 3:08:29 AM3/3/10
to doles, dev-tech-js-...@lists.mozilla.org
There's no standardized module loading facility in JavaScript; you need to roll your own that'll rely on Context.compileReader and then run the resulting script using Script.exec in the same scope where your main script runs. You'll need to deal with your own caching/reloading semantics etc.

Alternatively, you can check out latest CVS version and use the CommonJS support in it (look for org.mozilla.javascript.commonjs.module.Require class)

Attila.

--
home: http://www.szegedi.org
twitter: http://twitter.com/szegedi
weblog: http://constc.blogspot.com

Christian Grigis

unread,
Mar 3, 2010, 3:30:27 AM3/3/10
to
Hello,

We use Prototype in our server application, via the JSR-223 API, by
providing a host object that offers a "load()" method.

In other words:

public class FileLoader {
ScriptEngine engine;
Bindings bindings;

(...)

public Object load(String filePath) {
File path = new File(filePath);
Object result = engine.eval(new FileReader(path), bindings);

return result;
}

(...)
}

when initializing the bindings:

(...)
bindings.put("fileLoader", new FileLoader());
(...)

and from JavaScript you can then use:

(...)
fileLoader.load("prototype.js");
(...)

For Prototype, we modified the code so that the parts that require a
browser environment are essentially commented out.

This is the idea. To keep my post simple I left out many details, but if
you want, let me know and I can elaborate. :)

Hope this helps,

-Christian
--
Christian Grigis
Senior Software Engineer
NEXThink S.A. -- http://www.nexthink.com/

0 new messages