Richard G Baldwin (512) 223-4758, NRG Room 4238, Baldwin@DickBaldwin.com, http://www.austincc.edu/baldwin/

ITSE2321 Object-Oriented Programming

This material applies to classroom sections and Distance Learning sections taught by Professor Baldwin.

Compiling and/or Running Java from the Command Line

Fall 2014

Revised: 06/05/14

The official web page for this course is ITSE2321.htm

These instructions assume that you are running under Windows. If you are running under a different operating system, you will need to translate these instructions to fit that operating system.

Installing Java

Before you can compile and run Java programs, Oracle's Java Development Kit (JDK) must be installed on your computer. (Note that the JDK is already installed on the computers in the computer labs at NRG.)

The JDK can be downloaded for free from Oracle at the address provided in the course syllabus. For working at home, you will need to download and install the latest version of the JDK.

Testing your Java installation

Use your favorite text editor to create a text file named aacmd.bat in any folder on your disk. (Make sure the extension is bat and is not txt.) The file should contain the word cmd and that word only, on a line by itself.

Double click on the file named aacmd.bat. This should cause a command-line window to open that looks something like that shown in Figure 1:

Figure 1. A Windows command-line window.
C:\jnk\3>cmd
Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\jnk\3>

Typically this window will have a black background with white text. There should be a blinking cursor immediately to the right of the ">".

Type the javac command shown in Figure 2 immediately to the right of the ">" and press the Enter key. The response should be similar to that shown in Figure 2.

Figure 2. Testing the javac compiler.
C:\jnk>cmd
Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\jnk>javac -version
javac 1.7.0_25

C:\jnk>

In the event that you do not get this response, you will probably need to set the path (as opposed to the classpath) environment variable on your computer to point to something similar to the following:

C:\Program Files (x86)\Java\jdk1.7.0_25\bin

The format of this path will vary somewhat depending on which operating system you are running. The folder named bin should contain a file named javac.exe.

Enter the java command shown in Figure 3. The response should be similar to that shown in Figure 3.

Figure 3. Testing the java runtime engine (JRE).
C:\jnk>cmd
Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\jnk\6>java -version
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) Client VM (build 23.25-b01, mixed mode, sharing)

C:\jnk>

Getting a correct response for both tests is an indication that the Java JDK is properly installed on your computer.

If you enter either command and get something similar to that shown in Figure 4, you need to go back and troubleshoot the installation of the Java software. Pay particular attention to the need for a correct path environment variable.

Figure 4. Possible result for an incorrect Java installation.
C:\jnk\3>cmd
Microsoft Windows [Version 6.0.6001]
Copyright (c) 2006 Microsoft Corporation.  All rights reserved.

C:\jnk\3>javac -version
'javac' is not recognized as an internal or external command,
operable program or batch file.

C:\jnk\3>

Java documentation

If you have room on your disk, you should also download and install the Java documentation from the same web site.

If you don't have room on your disk for the documentation, you can view it online at http://docs.oracle.com/javase/8/docs/api/index.html 

Installing Ericson's media library

Installation instructions for Ericson's media library are provided on the CD that comes with the textbook. However, those instructions are oriented to the use of the DrJava IDE, whereas these instructions are oriented to the use of the command line. For purposes of these instructions, simply extract the folder named bookClasses from Ericson's zip file and store it on your hard disk.

In addition to the source code and compiled class files for the library, the bookClasses folder also contains a folder named doc that contains documentation for the library.

Compiling a Java program

The following instructions and the example commands that are shown below assume that:

Copy your file named aacmd.bat to the folder containing your program file named Main.java and double-click on the file named aacmd.bat. This should open a command-line window on your screen with a command prompt.

Enter a command similar to that shown in Figure 5 at the command prompt. (Don't forget to include the period and the semicolon immediately to the left of the M in Figure 5.)

Figure 5. Compiling a program named Main.java.
javac -cp .;M:\Ericson\bookClasses Main.java

You will need to replace some of the text shown in Figure 5 with the path to the location of the folder named bookClasses on your disk. (If your Java program doesn't need Ericson's media library, you can omit that text entirely.)

If the program compiles successfully, the command prompt will simply return after the compilation is complete and one or more files with an extension of .class will appear in the folder. (For example, if the name of your Java program is Main, then one of the files that will appear will be named Main.class.) If the program does not compile successfully, compilation errors will appear on the screen. Correct your source code and try again.

Running a compiled Java program

The following instructions and the example commands that are shown below assume that:

Go to the folder containing the compiled class files, copy your file named aacmd.bat into that folder, and double-click on your file named aacmd.bat. This should open a command-line window on your screen with a command prompt.

Enter a command similar to that shown in Figure 6.

Figure 6. Running the program named Main.
java -cp .;M:\Ericson\bookClasses Main

Once again, you will need to replace some of the text shown in Figure 6 with the path to the location of the folder named bookClasses on your disk. (If your Java program doesn't need Ericson's media library, you can omit that text entirely.)

If everything works as intended, this should cause your Java program to run.

Temporarily setting the path

The path environment variable does not get set automatically when you install or update the jdk. If you know how to do it, you can go to System -> Advanced System Settings on the Control Panel and set the path permanently. You probably shouldn't try that unless you know what you are doing.

A safer alternative, when running from the command prompt, is to execute a command similar to that shown in Figure 7 from the command prompt before you execute the commands to compile and run your program.

Figure 7. Temporarily setting the path.
path=%path%;C:\Program Files (x86)\Java\jdk1.7.0_05\bin

The portion of the command inside the % markers will save the current path. The portion after the semicolon will update the current path to include the path that you specify. The path environment variable will revert to its original form when you terminate the command-line session.

To avoid typing errors, I typically place the command in Figure 7 at the beginning of a batch file. I follow that with the commands from Figure 5 and Figure 6 and add a pause command at the end. Then I execute the batch file by double-clicking it in Windows Explorer.

-end-

File: ITSE2321CompilingAndRunningJavaFromCommandLine.htm