Quick Index
Overview


Scripts are another option to compile and run.

A script is simply a set of commands typed/grouped into a script file.

In Windows, scripts typically end in ".bat" (BAT files).
On the Mac/unix, scripts typically end in ".sh" (SH files).

We'll demonstrate the console by example.

Also see opening console and file browser as needed.

Example Script


Simple Script
The script is shown to the right and is simply a list of commands. It saves us time (we don't have to type into console each time).

  • The "REM" prefixed statements are script comments (in Windows scripts)
  • The javac command compiles the JAVA file
1
REM A script to compile "Greeter.java" REM Compile (build machine code from source code)
2
javac Greeter.java pause


Steps


Let's start by creating a directory with any name you like: e.g. "Examples", "Temp", "Scratch", etc (up to you). We'll call it the working directory
C:
 \---Work
     \---Scratch
Open this ZIP file Greeter.zip
MAC and Unix Scripts
Windows Scripts
Greeter.java
GreeterTester.java
README.TXT
Copy "Greeter.java" and "GreeterTester.java" from the ZIP to your working directory.
C:
 \---Work
     \---Scratch
         Greeter.java
         GreeterTester.java
If you use Windows, then open the Windows directory in the ZIP.
If you use MAC/Unix, then open MAC and Unix Scripts Scripts
MAC and Unix Scripts
Windows Scripts
Greeter.java
GreeterTester.java
README.TXT
Copy the three scripts from the ZIP into your working directory. Your working directory should now look like the one shown here (five files).

Note if using MAC/Unix they will the the scripts with extension ".sh"
C:
 \---Work
     \---Scratch
         Greeter.java
         GreeterTester.java
         Compile-Greeter.bat
         Compile-GreeterTester.bat
         Run-GreeterTester.bat
Compile the JAVA files by executing the Greeter script, then the GreeterTester script.
The scripts can be executed by double-clicking on each in the file browser (or right-click and choosing "Open").
C:
 \---Work
     \---Scratch
         Greeter.java
         GreeterTester.java
         Compile-Greeter.bat
         Compile-GreeterTester.bat
         Run-GreeterTester.bat
After compiling (running the two compile scripts) we should now have two new files:

  • Greeter.class
  • GreeterTester.class

These two files are the "results" of the compiles. They are ready to be "run" (which we'll do next).
C:
 \---Work
     \---Scratch
         Greeter.java
         Greeter.class
         GreeterTester.java
         GreeterTester.class
         Compile-Greeter.bat
         Compile-GreeterTester.bat
         Run-GreeterTester.bat
Now we're ready to "run" the program.
Execute "Run-GreeterTester.bat".
C:
 \---Work
     \---Scratch
         Greeter.java
         GreeterTester.java
         Compile-Greeter.bat
         Compile-GreeterTester.bat
         Run-GreeterTester.bat
And here is the output for the run.
C:\Work\Scratch>echo off

GREETINGS to you from a simple Java program!!

Press any key to continue . . .

Customizing Scripts


Note that the scripts can easily be customized for your own class. The steps would be like this: