Using Other Scripting Languages in Java
suggest changeVersions
[{“Name”:“Java SE 6”,“GroupName”:null},{“Name”:“Java SE 7”,“GroupName”:null},{“Name”:“Java SE 8”,“GroupName”:null}]
Introduction
Java in itself is an extremely powerful language, but its power can be extended further thanks to JSR223 (Java Specification Request 223), which introduces a script engine.
Remarks
The Java Scripting API enables external scripts to interact with Java
The Scripting API can enable interaction between the script and Java. The scripting languages must have an implementation of a script engine on the classpath.
By default, JavaScript (also known as ECMAScript) is provided by Rhino in Java SE 6 and 7, and by Nashorn in Java SE 8. Every script engine has a script context where all the variables, functions, and methods are stored in bindings. Sometimes you might want to use multiple contexts, as they support redirecting the output to a buffered Writer and errors to another.
There are many other script engine libraries like Jython and JRuby. As long as they are on the classpath you can eval code.
We can use bindings to expose variables into the script. We need multiple bindings in some cases, because exposing variables to an engine exposes them only to that engine. Sometimes we need to expose certain variables, such as the system environment and path, that are the same for all engines of the same type. In that case, we need a binding with global scope. Exposing variables to that binding exposes them to all script engines created by the same EngineFactory.