distribute commonjs script, and embed in Java

41 views
Skip to first unread message

jg

unread,
Jan 14, 2011, 4:03:57 AM1/14/11
to Narwhal and Jack
Hi,

this time, I would like to know the recommended way to distribute a
narwhal (very simple commonjs) script to be run as a standalone
application.
I can use a script with
java -cp path/to/narwhal/js.jar
org.mozilla.javascript.tools.shell.Main -e 'importPackage(java.io,
java.util,javax.swing,
java.awt,java.awt.event);importClass(java.lang.System)' -f path/to/
narwhal/engines/rhino/bootstrap.js -f path/to/some/lib.js -f path/to/
myfile.js

is that correct?

How can I embed the same command line into Java? should I use the
scripting API or the Rhino API as shown, for instance, in
http://www.envjs.com/doc/guides #embed :
I'm not familiar with neither of them.

import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.tools.shell.Global;
import org.mozilla.javascript.tools.shell.Main;

public class Test{
public static void main(String [] args){
Context cx = ContextFactory.getGlobal().enterContext();
cx.setOptimizationLevel(-1);
cx.setLanguageVersion(Context.VERSION_1_5);
Global global = Main.getGlobal();
global.init(cx);
Main.processSource(cx, "path/to/narwhal/engines/rhino/
bootstrap.js");
Main.processSource(cx, "path/to/some/lib.js");
Main.processSource(cx, "path/to/myfile.js");
}
}
I think the above solution is the prefered one, am I right?

Another question I have is about packages.json and jars. I haven't
been successful in putting a jar into a jars folder. It's never found
in the classpath! Could you provide a simple example?
say I do a "tusk init"...
I have my.jar in jars (containing org.example)
and in my js file I need to import org.example
var doit = function(){
importPackage(org.example);
...
}
For now I set the classpath the usual way, outside of packages.json.

Well, that's it!
Thanks for the project. I hope it's going to be active all year, and
happy new year for all Narwhal,Jack and other commonjs guys...
jg







Tom Robinson

unread,
Jan 18, 2011, 7:01:21 PM1/18/11
to narw...@googlegroups.com
You might want to look at how we do it in a Java WARs for ideas: https://github.com/tlrobinson/jack-servlet (check the src directory)

> --
> You received this message because you are subscribed to the Google Groups "Narwhal and Jack" group.
> To post to this group, send email to narw...@googlegroups.com.
> To unsubscribe from this group, send email to narwhaljs+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/narwhaljs?hl=en.
>

jg

unread,
Feb 1, 2011, 11:15:10 AM2/1/11
to Narwhal and Jack
Hi,

Thank you for your answer. I've checked your implementation out, and
I've been able to use the Rhino API. I hadn't seen some useful methods
like ScriptableObject.callMethod((Scriptable)
ScriptableObject.getProperty(require, "paths"), "push", new String[]
{path/to/lib});

Regards
jg

Bryan A

unread,
Feb 21, 2011, 9:48:00 AM2/21/11
to Narwhal and Jack
I am also trying to use narwhal with embedded rhino. Could you post
you solution?
Reply all
Reply to author
Forward
0 new messages