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 HelloWorld.java 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 HelloWorld.java
  3. Run the program with the following command:

    ./HelloWorld

    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:// java.sun.com/).

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:

http://java.sun.com/j2se/downloads.html

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:

    http://java.sun.com/j2se/downloads.html

    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:

    ./j2sdk*.bin

    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:

    /usr/java/j2sdk1.4.1_01

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:

PATH=$PATH:/usr/java/j2sdk1.4.1_01/bin

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

Name

Description

LICENSE

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.

COPYRIGHT

The copyright information for this release of the Java 2 SDK

README

Information about the Java 2 SDK contents

README.html

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

src.zip

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

bin

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

demo

Directory with many sample Java programs with source code

include

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

lib

Directory with Java class libraries

man

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