Subj : Java to Javascript using Rhino To : netscape.public.mozilla.jseng From : Aniruddha Thakur Date : Wed Sep 21 2005 12:37 pm This is a multi-part message in MIME format. ------_=_NextPart_001_01C5BEDB.7A203D26 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Hi, I am new to Javascript and am trying to invoke javascript from Java. I have js.jar from Rhino dowload. Here is my helloworld.js: =20 print("Hello World from " + arguments[0}); =20 Now, I can invoke this using javascript shell: % > java org.mozilla.javascript.tools.shell.Main scripts/helloworld.js anil Hello World from anil % > So far everything is fine. Now, I would like to use Java to execute the same. ------------------------------------------- Try#1: Directly invoking tools.Main.main(String[]) public void testHelloworld() { String s =3D "scripts/helloworld.js anil";=20 =20 Main.main(params); // int jsOut =3D Main.exec(params); // System.out.println("jsout =3D " + jsOut); }=20 =09 Output: Nothing was displayed on the screen. I used tools.debugger.Main and was able to step through my javascript which did what I wanted it to. However, I was unable to see any output in the window. Similarr results with Main.exec.=20 This may not be a right way to execute a script from Java. Since I read on the documentation and found out how to embed Javascript in Java using Rhino. Try#2: Using embedding tutorial from RunScript2.java public void testHelloworld() { Context context =3D Context.enter(); ScriptableObject scope =3D context.initStandardObjects(); =20 try { String inputStr =3D "scripts/helloworld.js anil"; =20 Object jsOut =3D Context.javaToJS(System.out, scope); ScriptableObject.putProperty(scope, "out", jsOut);=20 =09 Object result =3D context.evaluateString(scope, inputStr, "", 1, null);=20 System.out.println("Context.toString(result) =3D " + Context.toString(result));=20 =20 } finally { Context.exit(); } }=20 =09 Output: Exception indicating that evaluateString failed...=20 [junit] Testcase: testHelloworld took 0.218 sec [junit] Caused an ERROR [junit] missing ; before statement (#1) [junit] org.mozilla.javascript.EvaluatorException: missing ; before statemen t (#1) [junit] at org.mozilla.javascript.DefaultErrorReporter.runtimeError(Defa ultErrorReporter.java:95) [junit] at org.mozilla.javascript.DefaultErrorReporter.error(DefaultErro rReporter.java:82) [junit] at org.mozilla.javascript.Parser.addError(Parser.java:120)=20 [junit] at org.mozilla.javascript.Context.compileString(Context.java:1372) [junit] at org.mozilla.javascript.Context.compileString(Context.java:1361) [junit] at org.mozilla.javascript.Context.evaluateString(Context.java:1217) [junit] at com.outerbay.api.services.job.ActionTest.testHelloworld(ActionTest.java: 68) Certainly, I am not getting the 3rd argument. =20 Try#3: Using embedding tutorial from RunScript3.java public void testHelloworld() { Context context =3D Context.enter(); ScriptableObject scope =3D context.initStandardObjects(); try { Object fObj =3D scope.get("scripts/helloworld.js", scope); if (!(fObj instanceof Function)) { System.out.println("scripts/getJobInfo.js is undefined or not a function."); } else { System.out.println("scripts/helloworld.js is defined as a function."); Object functionArgs[] =3D { "anil" }; Function f =3D (Function)fObj; Object result =3D f.call(context, scope, scope, functionArgs); String report =3D "scripts/getHelloworld.js() =3D " + Context.toString(result); System.out.println(report); }=20 } finally { Context.exit(); } } As expected, I always go through the route of : undefined or not a function =20 What am I doing wrong? I am sure there are examples of this but I am unable to find that out. Any help would be appreciated. =20 Thanks, - Aniruddha =20 =20 ------_=_NextPart_001_01C5BEDB.7A203D26 Content-Type: text/html; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Message
Hi,
I am new to Javascript and am trying to = invoke=20 javascript from Java. I have js.jar from Rhino dowload. Here is my=20 helloworld.js:
 
      = print("Hello World=20 from " + arguments[0});
 
Now, I can invoke this using javascript = shell:
% > java=20 org.mozilla.javascript.tools.shell.Main scripts/helloworld.js = anil
Hello World from anil
% >
So far everything is fine. Now, I would = like to use=20 Java to execute the same.
-------------------------------------------

Try#1: Directly invoking tools.Main.main(String[])

 public void testHelloworld()=20 {
      String s =3D = "scripts/helloworld.js anil";=20
     
     =20 Main.main(params);
    // int jsOut =3D=20 Main.exec(params);
    // System.out.println("jsout = =3D " +=20 jsOut);
  }
=20
=20
Output: Nothing was displayed on the screen. I = used=20 tools.debugger.Main and was able to step through my javascript which = did what=20 I wanted it to. However, I was unable to see any output in the window. = Similarr results with Main.exec.
This may not be a right way to execute a script from Java. = Since I=20 read on the documentation and found out how to embed Javascript in = Java using=20 Rhino.
Try#2: Using embedding tutorial from=20 RunScript2.java
  public void=20 testHelloworld() {
    Context context =3D=20 Context.enter();
    ScriptableObject scope =3D=20 context.initStandardObjects();
  
=20
  =   try=20 {
      String inputStr =3D=20 "scripts/helloworld.js anil";
     
      Object jsOut =3D Context.javaToJS(System.out,=20 = scope);
      ScriptableObject.putProper= ty(scope,=20 "out", jsOut);
=20
=20
      Object = result =3D=20 context.evaluateString(scope, inputStr, "<cmd>", 1, = null);=20
     =20 System.out.println("Context.toString(result) =3D " +=20 Context.toString(result));=20
      =
    } finally=20 {
      = Context.exit();
   =20 }
  }
=20
=20
Output: Exception indicating that = evaluateString=20 failed...=20
    [junit] Testcase: testHelloworld took 0.218=20 sec
    [junit]     Caused an=20 ERROR
    [junit] missing ; before statement=20 (<cmd>#1)
    [junit]=20 org.mozilla.javascript.EvaluatorException: missing ; before = statemen
t=20 (<cmd>#1)
    [junit]     = at=20 = org.mozilla.javascript.DefaultErrorReporter.runtimeError(Defa
ultError= Reporter.java:95)
   =20 [junit]     at=20 = org.mozilla.javascript.DefaultErrorReporter.error(DefaultErro
rReporte= r.java:82)
   =20 [junit]     at=20 org.mozilla.javascript.Parser.addError(Parser.java:120)
    [junit]     at=20 = org.mozilla.javascript.Context.compileString(Context.java:1372)
 =   =20 [junit]     at=20 = org.mozilla.javascript.Context.compileString(Context.java:1361)
 =   =20 [junit]     at=20 = org.mozilla.javascript.Context.evaluateString(Context.java:1217)
 = ;  =20 [junit]     at=20 = com.outerbay.api.services.job.ActionTest.testHelloworld(ActionTest.java:6= 8)
        = Certainly, I=20 am not getting the 3rd argument.
 
Try#3: Using embedding tutorial from=20 RunScript3.java
public void testHelloworld()=20 {
    Context context =3D=20 Context.enter();
    ScriptableObject scope =3D=20 context.initStandardObjects();
    try = {
      Object fObj =3D=20 scope.get("scripts/helloworld.js",=20 scope);
      if (!(fObj instanceof = Function))=20 {
       =20 System.out.println("scripts/getJobInfo.js is undefined or not = a=20 function.");
      } else=20 {
       =20 System.out.println("scripts/helloworld.js is defined as a=20 function.");
        Object=20 functionArgs[] =3D { "anil" = };
       =20 Function f =3D = (Function)fObj;
       =20 Object result =3D f.call(context, scope, scope,=20 functionArgs);
        String = report =3D=20 "scripts/getHelloworld.js() =3D " +=20 = Context.toString(result);
       =20 System.out.println(report);
      = }=20
    } finally {
     =20 Context.exit();
    }
  }
As expected, I always go through the route of : undefined or = not a=20 function
 
What am I doing wrong? I am sure there are examples of this but I = am unable=20 to find that out.
Any help would be appreciated.
 
Thanks,
- Aniruddha
 
 
------_=_NextPart_001_01C5BEDB.7A203D26-- .