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

ITSE2317 Java Programming (Intermediate)

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

Compiling and/or Running Java from the Command Line

Summer 2010

Revised: 05/24/10

The official web page for this course is ITSE2317.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, Sun'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 Sun at http://java.sun.com/javase/downloads/index.jsp.  You will need to download and install the latest version of the JDK (without NetBeans and without Java EE).  As of the date of this writing (05/24/10), there are several download choices available at that web site.  The one that you need is worded as shown in Figure 1, but be aware that the wording may change over time.

Figure 1. Java software download.

JDK 6 Update 20 (JDK or JRE)

This release contains critical security updates to the Java runtime. Please update now to take advantage of these enhancements.   Learn more

What Java Do I Need? You must have a copy of the JRE (Java Runtime Environment) on your system to run Java applications and applets. To develop Java applications and applets, you need the JDK (Java Development Kit), which includes the JRE.

NOTE: The Firefox 3.6 browser requires Java SE 6 Update 10 or later. Otherwise, Java-based web applications will not work.

 

Just click the corresponding download button and follow the instructions from there.

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 2:

Figure 2. 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 3 immediately to the right of the ">" and press the Enter key.  The response should be similar to that shown.

Figure 3. 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.6.0_14

C:\jnk>

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

Figure 4. 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>java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) Client VM (build 14.0-b16, mixed mode, sharing)

C:\jnk>

Getting a correct response for both tests is an indication that Java is properly installed on your computer.  Note that the version shown in both cases should be 1.6.0_14 or later.

If you enter either command and get something similar to that shown in Figure 5, you need to go back and troubleshoot the installation of the Java software.

Figure 5. 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.  As of the date of this writing, the wording for the documentation download at http://java.sun.com/javase/downloads/index.jsp is as shown in Figure 6 but it may change over time.

Figure 6. Java documentation.

Java SE 6 Documentation

Again, just click the corresponding download button and follow the instructions from there.

If you don't have room on your disk for the documentation, you can view it online at http://java.sun.com/javase/6/docs/ 

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, you only need to do Step 5 from the installation instructions on the CD.  That step reads as shown in Figure 7.

Figure 7. Installing Ericson's media library.

STEP 5: COPY SAMPLE PROGRAMS AND MEDIA

1. Select the intro-prog-java folder and copy it to your hard disk.

The folder named intro-prog-java identified in Figure 7 contains the following two folders and nothing else:

If you prefer, you can simply copy those two folders to your disk.

(EXCEPTION:  Note however that although there is a copy of the media library on the CD in the back of the textbook, it is lacking some features and has a few known bugs.

You'll need to download the Java 1.5+ version: bookClasses10-1-07.zip.  This version has some new features as seen at New Features in 8-14-07 or later bookClasses.  For more information, see http://coweb.cc.gatech.edu/mediaComp-plan/101.

The zip file also contains a folder named intro-prog-java, which in turn contains a folder named bookClasses.  Use the contents of the bookClasses folder from the zip file instead of the contents of the folder having the same name on the CD from the back of the textbook.)

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 8 at the command prompt.

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

You will need to replace the text shown in red in Figure 8 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 the red text entirely.)  For example, if you are in an ACC NRG lab and you have set your system up according to the instructions provided in Running DrJava at ACC, you would enter the command shown in Figure 9 at the command prompt.

Figure 9. Compiling Main.java in ACC NRG lab.
javac -cp .;H:\MediaCompBookCD\bookClasses Main.java

If you are at home, you will need to make the substitution described above.

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 10.

Figure 10. Running the program named Main.
java -cp .;M:\Ericson\bookClassess Main

Once again, you will need to replace the text shown in red in Figure 10 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 the red text entirely.)  For example, if you are in an ACC NRG lab and you have set your system up according to the instructions provided in Running DrJava at ACC, you would enter the command shown in Figure 11 at the command prompt.

Figure 11. Running Main in ACC NRG lab.
java -cp .;H:\MediaCompBookCD\bookClasses Main

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

-end-

File: ITSE2317CompilingAndRunningJavaFromCommandLine.htm