RhinoHello.java - Rhino JavaScript Hello Example

Q

How to write a simple "Hello World!" example with Rhino JavaScript Java library? I have Rhino 1.7R5 installed.

✍: FYIcenter

A

If your have Rhino 1.7R5 installed, you can try the following example program, RhinoHello.java:

// Copyright (c) 2017 FYIcenter.com
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;

public class RhinoHello {
   public static void main(String[] args) throws Exception {
	  ContextFactory f = new ContextFactory();
      Context c = f.enterContext();
	  Scriptable s = c.initStandardObjects();

	  String js = "java.lang.System.out.println('Hello world!')";
      c.evaluateString(s, js, null, 1, null);
   }
}

Compile and run the example program, RhinoHello.java:

>\fyicenter\jdk-1.8.0\bin\javac -cp \fyicenter\rhino1_7R5\js.jar RhinoHello.java

>\fyicenter\jdk-1.8.0\bin\java -cp .;\fyicenter\rhino1_7R5\js.jar RhinoHello

Hello world!

 

Using Rhino JavaScript Library in Java Programs

⇒⇒FAQ for Rhino JavaScript Java Library

2017-08-13, 142👍, 0💬