Subj : RE: Java to Javascript using Rhino To : netscape.public.mozilla.jseng From : Aniruddha Thakur Date : Thu Sep 22 2005 10:41 am This is a multi-part message in MIME format. ------_=_NextPart_001_01C5BF94.7EC0BEE2 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Hi All, Update:=20 I was able to run the following from Java using shell.Main in a single-threaded environment. I get errors in multi-threaded evironment. % > java org.mozilla.javascript.tools.shell.Main scripts/helloworld.js anil=20 Hello World from anil=20 % > Here is Java--To--JSscript a class: javaToJS.java that calls tools.shell.Main.main. This part works fine in single threaded environment. Unfortunately, the second part below needs a few modifications to the script itself as i do not know how to pass any arguments (so, i hard coded in the script to test). package scripts; =09 =20 import java.io.FileReader;=20 import org.mozilla.javascript.Context;=20 import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.tools.shell.Main; =09 public class javaToJS { public static void main(String[] args) { Context context =3D Context.enter();=20 ScriptableObject scope =3D context.initStandardObjects(); =20 try { // Part#1: Using tools.shell.Main.main String[] params =3D { "scripts/helloworld.js", "anil" };=20 Main.main(params); // or Main.main(args)=20 =20 // Part#2: Using context and need this for Multi-threaded env to retrieve output FileReader fileReader =3D new FileReader("scripts/helloworld.js"); Object result =3D context.evaluateReader(scope, fileReader, "scripts/helloworld.js", 1, null);=20 System.out.println(Context.toString(result)); System.out.println("---javaToJS DONE----"); } catch (Throwable thr) { thr.printStackTrace(); } finally { Context.exit(); } } } =09 % > javac scripts/javaToJS=20 % > java scripts/javaToJS=20 Hello World from anil << from Part#1=20 % > java scripts/javaToJS << Using Part#2 (recompiled and modified the script)=20 org.mozilla.javascript.EcmaError: ReferenceError: "print" is not defined. (scripts/helloworld.js#1) at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3 240) at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3 230) at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:33 03) at org.mozilla.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntim e.java:1890) at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:92) at org.mozilla.javascript.gen.c1._c0(scripts/helloworld.js:1) at org.mozilla.javascript.gen.c1.call(scripts/helloworld.js) at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:304) at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2769) at org.mozilla.javascript.gen.c1.call(scripts/helloworld.js) at org.mozilla.javascript.gen.c1.exec(scripts/helloworld.js) at org.mozilla.javascript.Context.evaluateReader(Context.java:1251) at scripts.javaToJS.main(javaToJS.java:26)=20 ------------------------------------------------ Any help would be appreciated. Thanks, - Aniruddha =20 ---rest deleted--- =20 ------_=_NextPart_001_01C5BF94.7EC0BEE2 Content-Type: text/html; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Message
Hi=20 All,
Update:
I was able to run the following = from Java using=20 shell.Main in a single-threaded environment.
I get=20 errors in multi-threaded evironment.
% > java = org.mozilla.javascript.tools.shell.Main=20 scripts/helloworld.js anil
Hello World from anil=20
% >
Here=20 is Java--To--JSscript a class: javaToJS.java that calls=20 tools.shell.Main.main. This part works fine in single threaded = environment.=20 Unfortunately, the second part below needs a few modifications = to the=20 script itself as i do not know how to pass any arguments = (so, i=20 hard coded in the script to test).
package=20 scripts;
 
import java.io.FileReader;=20
import=20 org.mozilla.javascript.Context;=20
import = org.mozilla.javascript.ScriptableObject;
import = org.mozilla.javascript.tools.shell.Main;
public class=20 javaToJS {
    public static void main(String[] = args)=20 {
        Context = context =3D=20 Context.enter();
        ScriptableObject = scope =3D=20 context.initStandardObjects();
 
        try = {
       &nbs= p;   =20 // Part#1: Using = tools.shell.Main.main
       &nbs= p;    String[] params = =3D {=20 "scripts/helloworld.js", "anil" };
          &nbs= p;=20 Main.main(params); // or Main.main(args)
 
       &nbs= p;   =20 // Part#2: Using context and need this for = Multi-threaded=20 env to retrieve output
=
           &nb= sp;FileReader fileReader =3D new=20 FileReader("scripts/helloworld.js");
          &nbs= p;=20 Object result =3D context.evaluateReader(scope, fileReader,=20 "scripts/helloworld.js", 1, null); 
          &nbs= p;=20 System.out.println(Context.toString(result));
          &nbs= p;=20 System.out.println("---javaToJS DONE----");
         = } catch=20 (Throwable thr) {
          &nbs= p;=20 thr.printStackTrace();
        } finally=20 {
          &nbs= p;=20 Context.exit();
        = }
    }
}
% = > javac=20 scripts/javaToJS=20
% = > java =20 scripts/javaToJS=20
Hello = World from=20 anil << from=20 Part#1
%=20 > java scripts/javaToJS=20 << Using Part#2 (recompiled and modified the script)
org.mozilla.javascript.EcmaError: = ReferenceError:=20 "print" is not defined. (scripts/helloworld.js#1)
 at=20 = org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:32= 40)
 at=20 = org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:32= 30)
 at=20 = org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:330= 3)
 at=20 = org.mozilla.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime= ..java:1890)
 at=20 = org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:92)<= BR> at=20 org.mozilla.javascript.gen.c1._c0(scripts/helloworld.js:1)
 at = org.mozilla.javascript.gen.c1.call(scripts/helloworld.js)
 at=20 = org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:304)<= BR> at=20 = org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2769) at=20 org.mozilla.javascript.gen.c1.call(scripts/helloworld.js)
 at=20 org.mozilla.javascript.gen.c1.exec(scripts/helloworld.js)
 at=20 = org.mozilla.javascript.Context.evaluateReader(Context.java:1251)
 = ;at=20 scripts.javaToJS.main(javaToJS.java:26)
------------------------------------------------
Any = help would be=20 appreciated.
Thanks,
-=20 Aniruddha
 
---rest deleted---
 
------_=_NextPart_001_01C5BF94.7EC0BEE2-- .