Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titleback quote exec strings
// exec "ls -l" to get file listing as a string
var files = `ls -l`
var lines = files.split("\n");

// print only the directories
for (var l in lines) {
    var line = lines[l];
    if (line.startsWith("d")) // directory
        print(line)
}

Shell script style hash comments (-scripting mode only)

In -scripting mode, nashorn accept shell script style # (hash) single line comments.

Code Block
title# comment example
# style line comment -scripting mode
# prints hello

print("hello")

Nashorn supports shebang scripting. You need to create a link to jjs from your platform executable directory like /usr/bin as shown below.

Code Block
titleShebang scripts
$ cd /usr/bin
$ sudo ln -s $JAVA_HOME/bin/jjs jjs
$ chmod 755 test.js

where test.js is as follows:

#!/usr/bin/jjs
print("hello")

$ ./test.js

For shebang nashorn scripts, -scripting mode is automatically enabled.

Nashorn script API extensions

...