# Java Examples (Running ooRexx Scripts from Java)

Hint: if you wish to look at the ooRexx code with syntax highlighting, then you can use the free and open-source editor `"[g]vim"`, which is available for practically all platforms and can be downloaded from its homepage at http://www.vim.org/.

. <directory> (List of this directory)
  Display all of this directory's files and directories.
.. <directory> (List of parent directory)
  Display all of the parent directory's files and directories.
Nutshell_01_Eval.java (pure Rexx version transcribed from the Java program: RexxNutshell_01_Eval.rex)
  A Java demonstration program to show, how to setup and evaluate (invoke, cf. `ScriptEngine.eval()`) the Rexx program nutshell_01.rex, initially without an argument, then retrieving the compiled (tokenized) Rexx script and re-evaluate it, this time supplying arguments from Java to it.
  To run the Java program enter the following command into a command line window:
  `java Nutshell_01_Eval`
Nutshell_02_Invoke.java (pure Rexx version transcribed from the Java program: RexxNutshell_02_Invoke.rex)
  A Java demonstration program to show, how to setup and evaluate (invoke) the Rexx program nutshell_02.rex, fetching the ooRexx object it returns on the Java side and then use the `javax.script.Invocable` methods to send the Rexx object messages from Java (using `Invocable.invokeMethod(...)`), including arguments. In addition invoking ooRexx public routines from Java (using `Invocable.invokeFunction(...)`) gets demonstrated.
  For demonstration purposes the same method `javax.script.Invocable` methods get exercised that the ooRexx jsr223-implementation implemented into the compiled (tokenized) Rexx script support.
  To run the Java program enter the following command into a command line window:
  `java Nutshell_02_Invoke`
Nutshell_03_Interface.java (pure Rexx version transcribed from the Java program: RexxNutshell_03_Interface.rex)
  A Java demonstration program to show, how to setup and evaluate (invoke) the Rexx program nutshell_03.rex, fetching the ooRexx object it returns on the Java side and then use the `javax.script.Invocable` methods to use the public routines or the methods of the returned Rexx object to implement Java interfaces. This Java program uses for that purposes the `Invocable.getInterface(...)` method.
  For demonstration purposes the same method `javax.script.Invocable` methods get exercised that the ooRexx jsr223-implementation implemented into the compiled (tokenized) Rexx script support. To run the Java program enter the following command into a command line window:
  `java Nutshell_03_Interface`

  Hint: the external BSF function `BSFCreateRexxProxy(rexxObject, [slot], javaInterface...)` allows an easier and much more powerful creation of Java proxy objects, where the ooRexx methods implement the Java interface methods. In this case one can declare to Java that multiple Java interfaces are implemented and it is possible to define a Rexx slot-argument, that will get supplied each time a Rexx method gets invoked as a result of the Java interface method being run on the Java side.

Nutshell_04_Eval.java (pure Rexx version transcribed from the Java program: RexxNutshell_04_Eval.rex)
  A Java demonstration program to show, how to setup and evaluate (invoke) the Rexx programs, use the `ScriptContext` to define the values the Rexx programs should use and executes three Rexx programs demonstrating the different possibilities to fetch the values:
  - nutshell_04a.rex: fetches the `java.awt.Dimension` objects named `d1`, `d2`, `d3`, `d4`, `d5`, `d6`, `d7`, `d8` from the supplied `ScriptContext` which is always supplied as an entry named `"ScriptContext"` in the `slotDir` argument (a Rexx directory that the `RexxScriptEngine` appends as the last argument).
  - nutshell_04a.rex: uses the Rexx external function `BSFContextVariables()` and the `JSR223` (defined in `BSF.CLS`) class method named `ScriptContext2RexxDir` to create Rexx context variables for all the values available in the supplied `ScriptContext` which is always supplied as an entry named `"ScriptContext"` in the `slotDir` argument (a Rexx directory that the `RexxScriptEngine` appends as the last argument).
  - nutshell_04c.rex: simply fetches the values (Java objects) with the Rexx script annotation `"/*@get()*/"` which causes all attributes from all `"ScriptContext Bindings"` to be made immediately available as local Rexx variables by the same name.
  - nutshell_04d.rex: simply fetches the values (Java objects) directly as Rexx arguments and uses them directly. In addition fetches the supplied `ScriptContext` which is always supplied as an entry named `"ScriptContext"` in the `slotDir` argument (a Rexx directory that the `RexxScriptEngine` appends as the last argument).
  To run the Java program enter the following command into a command line window:
  `java Nutshell_04_Eval`
RunScript.java (pure Rexx version transcribed from the Java program: RexxRunScript.rex)
  A simple Java utility to learn about the available script engines, their properties, test them, but also to allow running any script using the appropriate Java script engine.
  To run the Java program enter the following command into a command line window:
  `java RunScript`
    lists all available Java script engines
  `java RunScript -h`
    help: lists how this utility can be invoked
  `java RunScript -i`
    information: lists all available Java script engines and shows their properties
  `java RunScript -t`
    test: lists all available Java script engines and creates with the help of their factories "hello"-statements that get executed by the engines
  `java RunScript filename [argument...]`
    executes the script stored in `filename` loading the Java script engine with the help of the `javax.script.ScriptEngineManager`, using the extension in the `filename`. If one or more blank delimited `argument...` were supplied, they will be submitted to the script via the `ARGV` entry in the `ScriptContext`. The ooRexx `RexxScriptEngine` will use this array object to create the appropriate arguments for the Rexx script.
  `java RunScript -e engine filename [argument...]`
    executes the script stored in `filename` using the Java script engine supplied after the `-e` switch named `engine` with the help of the `javax.script.ScriptEngineManager`. If one or more blank delimited `argument...` were supplied, they will be submitted to the script via the `ARGV` entry in the `ScriptContext`. The ooRexx `RexxScriptEngine` will use this array object to create the appropriate arguments for the Rexx script.

Rony G. Flatscher, 2015-06-04, 2016-12-07, 2017-02-10, Wien/Vienna, Austria, Europe