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:

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

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.

Table of Contents

 About This Book

 Introduction to JavaScript

 ECMAScript Language Specification and JavaScript Dialects

 Data Types, Variables and Expressions

 Flow Control Statements

 Creating, Accessing, and Manipulating Arrays

 Defining and Calling Functions

 Web Browser Supporting JavaScript

 Server-Side and Client-Side Web Scripting

 Introduction to Objects

 Defining Your Own Object Types

 Inheritance of Properties and Methods through the Prototype Object Chain

'jrunscript' - JavaScript Shell Command from JDK

 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'

 Using Functions as "Function" Objects

 Introduction to Built-in Object Types

 W3C's Document Object Model (DOM) Specifications

 References

 PDF Printing Version