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

Running a JavaScript Code File with 'jrunscript'

This section provides a tutorial example on how to run a JavaScript code file with 'jrunscript' in batch mode.

Probably the best way to use "jrunscript" is to run it in batch mode, where is JavaScript code file is specified at the command line. "jrunscript" will process the entire code file in one shot.

In order to test "jrunscript" in batch mode, I revised one of my existing JavaScript Web page file into a pure JavaScript code file, For_Loop_Statements.js:

// For_Loop_Statements.js
// Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/

   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)
//       document.write("Found a prime number: " + i + ".\n");
         print("Found a prime number: " + i + ".\n");
   }

Notice that, I have to change "document.write()" method to "print()" method, because "document" object is only available, if the script is executed in a Web browser. Here is how I ran this script file with "jrunscript":

C:\herong>\progra~1\java\jdk1.6.0_02\bin\jrunscript 
   -f For_Loop_Statements.js

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.

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 a JavaScript Code File with 'jrunscript'