Javascript - Rhino Engine

About

Rhino 1) 2) 3) is an javascript engine that implements JavaScript ECMA from the Mozilla Foundation (It was the first JDK offering for JavaScript).

Compatibility / Error

If you have any error, check the compatibility table to see if the javascript statement is supported.

You can not run all statements or you need to install a polyfill

Example

rhino
Rhino 1.7.9 2018 03 15

print('Hello, World!');
Hello, World!

Usage

  • Rhino is used to run Javascript code
  • Rhino is also used to parse javascript and shorten the results for instance. See minifer such as YUICompressor, shrinksafe, …

Installation

Manual

Download the jar from github

Then create a shell script

  • Dos
@echo off
@SET SCRIPT_PATH=%~dp0
java -jar %SCRIPT_PATH%\rhino.jar %*
  • Bash
#!/bin/bash
java -jar rhino.jar "$@"

Linux

There is a linux package available

# ubuntu
apt-get install rhino
# centos
yum install rhino
Get:1 http://archive.ubuntu.com/ubuntu focal/universe amd64 libjline-java all 1.0-2 [69.4 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/universe amd64 librhino-java all 1.7.7.1-1 [1150 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/universe amd64 rhino all 1.7.7.1-1 [14.0 kB]
Fetched 1233 kB in 1s (2274 kB/s)
Selecting previously unselected package libjline-java.
(Reading database ... 35634 files and directories currently installed.)
Preparing to unpack .../libjline-java_1.0-2_all.deb ...
Unpacking libjline-java (1.0-2) ...
Selecting previously unselected package librhino-java.
Preparing to unpack .../librhino-java_1.7.7.1-1_all.deb ...
Unpacking librhino-java (1.7.7.1-1) ...
Selecting previously unselected package rhino.
Preparing to unpack .../rhino_1.7.7.1-1_all.deb ...
Unpacking rhino (1.7.7.1-1) ...
Setting up libjline-java (1.0-2) ...
Setting up librhino-java (1.7.7.1-1) ...
Setting up rhino (1.7.7.1-1) ...
update-alternatives: using /usr/bin/rhino to provide /usr/bin/js (js) in auto mode
Processing triggers for man-db (2.9.1-1) ...

man rhino
NAME
       rhino - invokes the JavaScript shell for running scripts in batch mode or interactive

SYNOPSIS
       rhino [options] script_filename_or_url [script_arguments]

DESCRIPTION
       This  manual  page documents briefly the rhino command.  This manual page was written for the Debian distribu‐
       tion because the original program does not have a manual page. It is written according to the html  documenta‐
       tion.

       rhino  is  a  start  script for the rhino JavaScript shell which provides a simple way to run scripts in batch
       mode or an interactive environment for exploratory programming.

OPTIONS
       -w     Enable warnings.

       -version versionNumber
              Specifies the language version to compile with. The string versionNumber must be one of 100, 110,  120,
              130,  140, 150, 160, 170, 180 or 200. See JavaScript Language Versions for more information on language
              versions.

       -opt, -O optLevel
              Optimizes at level optLevel, which must be an integer between -1 and 9. -1 means Interpretive  mode  is
              always used. 0 means no optimizations whereas 9 means all optimizations are performed.

       -f script_filename_or_url
              Reads script_filename_or_url content and execute it as a JavaScript script.

       -e script_source
              Executes script_source as a JavaScript script.

       -modules uri
              Add a single path or URL element to the CommonJS module search path. (implies -require)

       -require
              Enable CommonJS module support.

       -sandbox
              Enable CommonJS sandbox mode. (implies -require)

       -debug Generate debug code.

       -strict
              Enable strict mode.

       -fatal-warnings
              Treat warnings as errors.

       -encoding charset
              Use specified character encoding as default when reading scripts.

PREDEFINED PROPERTIES
       Scripts executing in the shell have access to some additional properties of the top-level object.

       arguments
              The  arguments object is an array containing the strings of all the arguments given at the command line
              when the shell was invoked.

       help() Executing the help function will print usage and help messages.

       defineClass(className)
              Define an extension using the Java class named with the string argument className.  Uses  ScriptableOb‐
              ject.defineClass() to define the extension.

       deserialize(filename)
              Restore from the specified file an object previously written by a call to serialize.

       load([filename,...])
              Load  JavaScript  source files named by string arguments. If multiple arguments are given, each file is
              read in and executed in turn.

       loadClass(className)
              Load and execute the class named by the string argument className. The class must be a class  that  im‐
              plements the Script interface, as will any script compiled by jsc.

       print([expr...])
              Evaluate  and print expressions. Evaluates each expression, converts the result to a string, and prints
              it.

       readFile(path[,characterCoding])
              Read given file and convert its bytes to a string using the specified character coding or default char‐
              acter coding if explicit coding argument is not given.

       readUrl(url[,characterCoding])
              Open an input connection to the given string url, read all its bytes and convert them to a string using
              the specified character coding or default character coding if explicit coding argument is not given.

       runCommand(commandName,[arg,...][options])
              Execute the specified command with the given argument and options as a separate process and return  the
              exit status of the process. For details, see JavaDoc for org.mozilla.javascript.tools.shell.Global#run‐
              Command.

       serialize(object,filename)
              Serialize the given object to the specified file.

       spawn(functionOrScript)
              Run the given function or script in a different thread.

       sync(function)
              creates a synchronized function (in the sense of a Java synchronized method) from an existing function.
              The new function synchronizes on the this object of its invocation.

       quit() Quit  shell.  The  shell will also quit in interactive mode if an end-of-file character is typed at the
              prompt.

       version([number])
              Get or set JavaScript version number. If no argument is supplied, the current  version  number  is  re‐
              turned.  If an argument is supplied, it is expected to be one of 100, 110, 120, 130, or 140 to indicate
              JavaScript version 1.0, 1.1, 1.2, 1.3, or 1.4 respectively.

SEE ALSO
       The online documentation under https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Shell

AUTHOR
       This manual page was written by Wolfgang Baer <[email protected]>, for the Debian project (but may be used by  oth‐
       ers).

Syntax

To run on Rhino, first download the Rhino JAR file from http://www.mozilla.org/rhino/.

java -jar js.jar -?
Usage: java org.mozilla.javascript.tools.shell.Main [options...] [files]
Valid options are:
    -?, -help          Displays help messages.
    -w                 Enable warnings.
    -version 100|110|120|130|140|150|160|170|180
                       Set a specific language version.
    -opt [-1|0-9]      Set optimization level.
    -f script-filename Execute script file, or "-" for interactive.
    -e script-source   Evaluate inline script.
    -modules [uri]     Add a single path or URL element to the CommonJS
                       module search path. (implies -require)
    -require           Enable CommonJS module support.
    -sandbox           Enable CommonJS sandbox mode. (implies -require)
    -debug             Generate debug code.
    -strict            Enable strict mode warnings.
    -fatal-warnings    Treat warnings as errors.
    -encoding charset  Use specified character encoding as default when reading scripts.

How to wrap a javascript file in a Shell script to run it directly ?

Below, you can see how you can create shell script in order to run directory a javascript file

  • Batch wrapper
@echo off
java -jar rhino.jar script.js %*
  • Linux/Mac
#!/bin/bash
java -jar rhino.jar script.js "$@"

Gradle

Example

dependencies {
    implementation(library("rhino"))
}





Discover More
Java Conceptuel Diagram
Java - Scripting

AspectJ BeanShell Ceylon Clojure Duby Fantom Groovy Ioke Jaskell JRuby Jython Noop Scala XTend Groovy en Scala are in Java 7 supported. Javascript: Rhino, ...
Javascript - (Interpreter|Engine|Runtime)

Javascript Interpreter (JavaScript_engineJavaScript engine) is a software which interprets and executes JavaScript code. Javascript can run in two different environment: within a browser or without...
Javascript - Asynchronous Module Definition (AMD)

The AMD specifies a mechanism for defining modules such that: the module and its dependencies can be specified and loaded asynchronously. ie Both the module and dependencies can be asynchronously...
Javascript - Linter / Minification (EsLint / Tern / Uglify)

This page is linter / minification in Javascript Linter are used under the hood by bundler in the optimization phase. Name Language Description eslint/eslintESLint Javascript A linter (identifying...
Javascript - Nashorn Engine

Nashorn is the JDK’s built-in JavaScript engine. Rhino was its predecessor. Background: The development of Nashorn started in late 2010 to experiment with the invokedynamic (292JSR 292) byte-code instruction....
Browser
Web - Headless browser (Test automation)

A headless browser is an application/library that emulates a web browser but without a graphical user interface ie (without DOM / without the Web api) They are the basis to build a web bot. Build...



Share this page:
Follow us:
Task Runner