Java Reference
In-Depth Information
Listing 1-2: The Contents of the helloscripting.js File
// helloscripting.js
// Print a message on the standard output
print('Hello Scripting!');
The following command executes the script stored in the helloscripting.js file
assuming that the file is stored in the current directory:
C:\>jjs helloscripting.js
Hello Scripting!
C:\>
If this command gives you an error similar to the following, it means that the
command was not able to find the specified file and you need to specify the full path of
the helloscritping.js file:
java.io.FileNotFoundException: C:\helloscripting.js (The system cannot find
the file specified)
The jjs command-line tool is a big topic and I will devote a complete chapter to it.
I will discuss it in detail in Chapter 10.
Printing Text in Nashorn
Nashorn provides you three functions to print text on the standard output:
print() function
The
printf() function
The
echo() function
The print() function is a varargs function. You can pass any number of arguments
to it. It converts its arguments to string and prints them separating them by a space.
At the end, it prints a new line. The following two invocations of the print() function
are the same:
The
print("Hello", "World!"); // Prints Hello World!
print("Hello World!"); // Prints Hello World!
The printf() function is used to use the printf-style formatted printing. It is the
same as invoking the Java method System.out.printf() :
printf("%d + %d = %d", 10, 20, 10 + 20); // Prints 10 + 20 = 30
 
Search WWH ::




Custom Search