Getting Ready for Java Programming

Getting Ready for Java Programming

Like any programming language, the best way to learn Java is to begin writing some programs. Of course, you need an overview of the language as well, but if you already know C++ and understand the basics of object-oriented programming, you can quickly become proficient in Java.

This section gets you started with the hands-on aspects of Java programming by briefly describing GCJ-the GNU Java compiler that comes with Red Hat Linux. Then, it describes how to download and install the Java Development Kit (JDK).

Using GCJ

Red Hat Linux comes with GCJ-the GNU Compiler for the Java Programming Language. GCJ should be installed if you selected the Development Tools package during Red Hat Linux installation.

You can use the GCJ to compile and run simple Java programs. For example, to build a simple Java program that prints Hello, World!, follow these steps from a text console or a terminal window (I explain Java programming further in the latter part of this chapter):

  1. Use a text editor such as vi or Emacs to create a file named and add the following lines to that file:

    public class HelloWorld {
        public static void main(String [] args) {
            System.out.println("Hello, World!");
  2. Compile and create an executable program named HelloWorld by running gcj (that's the name of the GNU Java compiler's executable) with the following command line:

    gcj --main=HelloWorld -o HelloWorld
  3. Run the program with the following command:


    The program should display the following line of output:

    Hello, World!

You can compile more complex Java programs using GCJ, but you run into a roadblock because GCJ does not yet include all the Java libraries.

Insider Insight 

Although Java code can theoretically run wherever a JVM is available, all but the simplest Java applications also require one or more of the Java libraries that may not necessarily be available in a JVM. The Java libraries provide the application programming interfaces (APIs) that Java programs use to perform tasks such as displaying graphical output or exchanging data over the network. To run typical Java programs, at minimum you need the libraries that come with Sun's Java 2 Standard Edition (J2SE). This means that you need the JVM and the JJ2SE libraries to develop Java applications. You can download the latest J2SE by following the links at Sun's Java home page (http://

Downloading and Installing Java 2 SDK

To develop Java applications, you need the Java 2 Software Development Kit. You can download the latest version of this kit by following the links at the following website:

To download and install J2SE SDK for Linux, follow these steps from your Red Hat Linux system:

  1. Make sure that your system is connected to the Internet. Start the Web browser and point the Web browser to the following URL:

    Click the link for the latest version of J2SE SDK.

  2. Select the SDK download link for the item labeled 'Linux RPM in self-extracting file.' Read the license agreement and, if you agree, click Accept.

  3. Click the link for the J2SE SDK file, and save it a convenient directory on your Red Hat Linux system (for example, /usr/local). The SDK is typically close to 40 MB in size, so the download can take a while if you have a slow dial-up Internet connection. After the download is completed, you end up with a file whose name ends in .rpm.bin. This file is a shell script that you must execute to extract the RPM file for J2SE SDK.

  4. From a terminal window, change directory to where you saved the J2SE SDK file and type the following command to make that file executable:

    chmod +x j2sdk*.bin
  5. Execute the file by typing the following command:


    This displays the license agreement a page at a time. After reading each page, press Enter to move forward. After the last page, you see the following prompt:

    Do you agree to the above license terms? [yes or no]
  6. Type yes, and press Enter. This starts the unpacking and ends with an RPM file.

  7. Install the RPM file with the following command:

    rpm -ivh j2sdk*.rpm

    This command installs the Java 2 SDK in the /usr/java directory under a subdirectory whose name depends on the version you are installing. For example, when I installed J2SE SDK version 1.4.1, everything is installed in the following directory:


Set the PATH environment variable to include the directory where the Java 2 SDK binaries are located. For example, if you install the Java 2 SDK in the /usr/java/j2sdk1.4.1_01 directory, add the following lines at the end of the /etc/profile file:


Taking Stock of the Java 2 SDK

The Java 2 SDK comes with a number of files organized into several directories. The top-level directory contains the files and subdirectories shown in Table 26-1.

Table 26-1: Java 2 SDK Content Summary




The license agreement under which Sun Microsystems is providing you the Java 2 SDK. You should read this file before using the Java 2 SDK.


The copyright information for this release of the Java 2 SDK


Information about the Java 2 SDK contents


HTML version of README file with links to Sun's websites

JDK source code in a ZIP archive. You don't need the source files to write Java applets or applications.


Directory containing the executable programs. You should add the full pathname of this directory to your PATH environment variable.


Directory with many sample Java programs with source code


Directory with the header files for the Java 2 SDK source (you don't need these to write Java programs)


Directory with Java class libraries


Directory with man pages for Java 2 SDK tools such as the javac compiler and java-the Java interpreter

The tools you'll use most are in the bin subdirectory. Here are some of the tools that you'll use when creating Java applications:

  • appletviewer-Java applet viewer program to load and run Java applets that are designed to be embedded in an HTML document (you can also load applets in a Java-capable browser by embedding the applet in an HTML document using the <applet> tag).

  • java-Java interpreter that runs standalone Java programs specified by a class name (it expects to find the class in a file with same name as the class, but with a .class extension). Standalone Java applications are Java classes with a main method (in Java, functions are called methods).

  • javac-Java compiler that converts Java source code into 'byte code' (that's the instruction set of the Java Virtual Machine). The compiler takes source code in a .java file and generates compiled code in a .class file.

  • jdb-Java debugger with a command syntax like that of the Linux gdb debuggers.

  • javadoc-Java API documentation generator that extracts documentation from source files with special comments that begin with /** and ends with a */.

  • javah-Java C file generator that generates C .h and .c files for a Java class. Such C files are needed to implement the native methods that make the APIs work on a particular operating system.

  • javap-Java class file disassembler that takes a class name and prints a human-readable description of the class.

Of these programs, the three that you'll use most are the following:

  • You'll use javac a lot because that's the Java compiler program. The javac program converts a Java source file (you'll see some examples in the next section) into a binary file that contains Java byte code-the instruction set of the Java Virtual Machine.

  • If you write a standalone Java application-one that can run without being embedded in a Web browser, you'll use the Java interpreter program, java, to run the application.

  • If you write Java applets-miniapplications that run inside a Web browser, you'll use the appletviewer program to test the applets. Of course, you can also test applets using a Java-capable Web browser such as Mozilla (after you install the Java plug-in).