JavaScript Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 2.11

Running JavaScript Code with 'jrunscript'

This section describes 3 ways to run JavaScript code with the scripting shell, 'jrunscript': interactive mode, batch mode and command line mode.

The JDK script shell, "jrunscript", allows you to run JavaScript codes in three ways:

  • "jrunscript" - Interactive mode: Running "jrunscript" without "-e" or "-f" option tells "jrunscript" to run in interactive mode. JavaScript code will be entered and evaluated one line as a time from the standard input.
  • "jrunscript -e <script_code>" - Command line mode: With the "-e" option, "jrunscript" will take script code only from the command line
  • "jrunscript -f <script_file_name>" - Batch mode: In this mode, "jrunscript" will read script code from the specified script file.

Let's try the command line mode first. There are 2 challenges when entering JavaScript code in the command line:

  • The entire code must be entered in a single line. It is ok to enter multiple statements in the command line. But they must be entered in one line without any line breaks.
  • On Windows system, the entire code must be enclosed in double quotes (""). This may cause problems if you are using double quotes inside your JavaScript Statements.

Here are some examples of running "jrunscript" to evaluate JavaScript codes specified in command line:

C:\herong>\progra~1\java\jdk1.6.0_02\bin\jrunscript 
   -e "print('Hello World!');"

Hello World!


C:\herong>\progra~1\java\jdk1.6.0_02\bin\jrunscript 
   -e "msg='Hello World!'; print(msg);"

Hello World!


C:\herong>\progra~1\java\jdk1.6.0_02\bin\jrunscript 
   -e "var i, j, is_prime; for ( i=3; i<=30; i+=2 ) { is_prime = true;
   for ( j=2; j<=i/2; j++) { is_prime = i%j > 0; if (!is_prime) 
   break;} if (is_prime) println('Found a prime number: ' + i); }"

Found a prime number: 3
Found a prime number: 5
Found a prime number: 7
Found a prime number: 11
Found a prime number: 13
Found a prime number: 17
Found a prime number: 19
Found a prime number: 23
Found a prime number: 29

Note that all "jrunscript" commands were entered in a single command line. I split them into multiple lines because the book page width is limited.

Sections in This Chapter

Downloading and Installing J2SE 1.6.0 on Windows

"jrunscript" - Scripting Shell Command and Options

Running JavaScript Code with 'jrunscript'

Evaluating JavaScript Code with 'jrunscript' Interactively

Running a JavaScript Code File with 'jrunscript'

Dr. Herong Yang, updated in 2008
Running JavaScript Code with 'jrunscript'