Java Reference
In-Depth Information
You want to enable your JavaScript file to become executable.
Solution
Make a JavaScript file executable by adding a shebang ( #! ) as the first line of the
script, followed by the path to the location of the jjs executable. In the following ex-
ample, a very simple JavaScript file is made executable by the inclusion of a shebang,
which points to the symbolic link of the jjs tool.
#! /usr/bin/env jjs
print('I am an executable');
To execute the script, it must be given the proper permissions. Apply the chmod
a+x permissions (in Unix) to make the script executable.
chmod a+x src/org/java8recipes/chapter18/recipe18_10/
jsExecutable.js
The script can now be invoked as an executable, as shown in the following com-
mand:
Juneau$ ./src/org/java8recipes/chapter18/recipe18_10/
jsExecutable.js
I am an executable
How It Works
To make a script executable, you simply add a shebang to the first line. The shebang is
used in Unix-based operating systems to tell the program loader that the script's first
line should be treated as an interpreter directive, and that the script should be passed to
that interpreter for execution. In the solution to this recipe, the first line of the script
tells the program loader that the script's contents should be executed using the jjs
tool:
#! /usr/bin/env jjs
Search WWH ::




Custom Search