Execute JavaScript using Nashorn in the Console

Execute JavaScript using Nashorn in the Console

To execute JavaScript using Nashorn in the console, you can use the jjs command, like this:

jjs -scripting myscript.js

In this example, the jjs command is used to run the myscript.js script using the Nashorn engine. The -scripting option indicates that the script is using the scripting mode, which allows you to use the Nashorn engine to execute JavaScript code directly from the command line.

Alternatively, you can use the -cp option to specify the classpath and include the Nashorn jar file, like this:

jjs -cp $JAVA_HOME/lib/nashorn.jar myscript.js

In this example, the Nashorn jar file is added to the classpath using the -cp option, and the myscript.js script is executed using the Nashorn engine.

Note: These examples are for running JavaScript scripts using Nashorn from the command line. For more information on the jjs command and its options, refer to the Nashorn documentation.