Last Updated:

Get started with Java programming

Installing the Java SDK

 

The original purpose of the Java language was to enable programmers to write a single program that could run on any platform. This goal can be expressed by the aphorism "Write Once, Run Anywhere" (WORA). It's not really that simple, but that's where it goes. Various components of Java technology support this goal. The Java platform comes in three editions: Standard, Enterprise, and Mobile (the latter two are for mobile device development). We will work with J2SE, which includes all the major Java libraries. All you need to do is download and install it.

 

To download the J2SE SDK (software development kit), follow these steps:

  1. Open a browser and go to the Java Technology page. At the top middle of the page, you'll see links to various Java technology subject areas. Select J2SE (Core/Desktop).
  2. In the list of current J2SE versions, select J2SE 1.4.2.
  3. In the left navigation bar of the page that appears, select Downloads.
  4. There are several downloadable packages on this page. Find and select the Download J2SE SDK link.
  5. Confirm the license terms and click Continue.
  6. You'll see a list of downloadable packages for different platforms. Select the appropriate package for your system.
  7. Save the file to your hard drive.
  8. After the download is complete, run the SDK installer on your hard drive, preferably in a folder with a convenient name in the root of the disk.

All! You now have a Java environment. The next step is to install the integrated development environment (IDE).

Install Eclipse

 

The integrated development environment (IDE) hides most of the routine technical details of working with the Java programming language, so you can concentrate on writing and running code. The JDK you just installed has several command-line tools that provide the ability to compile and execute Java programs without an IDE, but using these tools quickly becomes a headache for all programs that are not too simple. Using an IDE hides details, provides tools to speed up and improve your work, and is simply a more convenient way to develop programs.

Now you don't have to pay for an excellent IDE. The Eclipse IDE is an open source project that you can download and use for free. Eclipse stores and tracks your Java code in files located in your file system. You can also use Eclipse to work with code located in a CVS repository. The good news is that Eclipse allows you to work with the files you need, but hides the details of the files when working with various Java constructs, such as classes (which we'll cover in detail later).

Downloading and installing Eclipse is easy. Follow these steps:

  1. Open a browser and go to the Eclipse Web site.
  2. Click the Downloads link on the left side of the page.
  3. Click the Main Eclipse Download Site link to go to the Eclipse project download page.
  4. You'll see a list of build version types and names. Click link 3.0.
  5. In the middle of the page, you'll see a list of Eclipse SDKs for different platforms; select the appropriate version for your platform.
  6. Save the file to your hard drive.
  7. After the download is complete, run the installer and install Eclipse on your hard drive, preferably in a directory with a convenient name in the root of the disk.

All that's left is to configure the IDE.

Set up Eclipse

To use Eclipse when writing Java code, you must specify eclipse where the Java platform is located on your machine. Follow these steps:

  1. Launch Eclipse by double-clicking on an eclipse.exe file or an equivalent file for your platform.
  2. When the Welcome screen appears, click the Go To The Workbench link. This will take you to what's called the Resource perspective (more on that later).
  3. Select Window>Preferences>Installed JREs, which will allow you to specify the location of your Java environment installed on your system (see Figure 1).
    Java Programming

    Rice. 1. Eclipse Settings

     

  4. Eclipse will find the Java Runtime Environment (JRE) installed, but you must explicitly specify one of them installed in the "Installing the Java SDK" section. You can do this in the Preferences dialog box. If Eclipse displays an existing JRE, select it and click Edit; otherwise, click Add.
  5. Specify the path to the JRE JDK folder that you set in the Installing the Java SDK section.
  6. Click OK.

Eclipse is now configured to compile and run Java code. In the next section, we'll take a brief tour of the Eclipse environment to familiarize you with the program.

A brief tour of Eclipse

Working with Eclipse is a broad topic, and it's mostly beyond the scope of this article. Here we will consider only the most basic necessities for getting acquainted with the work of the Eclipse environment and its use for Java development.

When you start Eclipse, you get into the Resource perspective (Eclipse offers a set of perspectives for your code). The Resource perspective shows your file system in the Eclipse workspace you are using. The workspace stores all files related to Eclipse development. At this time, there is nothing in your workspace that you need to worry about.

Generally speaking, Eclipse has perspectives containing views. In the Resource perspective, you will see the Navigator view, the Outline view, etc. You can optionally move these views to any position on the screen. Eclipse is an unrestrictedly configurable environment, although for now we only need the default placement to work. But what we see doesn't allow us to do what we want. The first step to writing Java code in Eclipse is to create a Java project. This is not a construct of the Java language; it's just an Eclipse construct that lets you organize your Java code. To create a Java project, follow these steps:

  1. Select File>New>Project to display the New Project wizard (Figure 2). It's actually a "master of the masters"; in other words, it is a wizard that allows you to select the wizards to use (New Project wizard, New File wizard, etc.).
    new Project Wizard

    Rice. 2. New project wizard

     

  2. Select Java Project and click Next.
  3. Enter a name for the project (for example, "Intro"), leave all the default settings selected, and click Finish.
  4. For now, Eclipse should ask you if you want to switch to the Java perspective. Click No.

You've just created a Java project called Intro, which you should see as Navigator in the upper-left corner of the screen. We didn't switch to the Java perspective after creating the project because there is a more appropriate perspective for our current purposes. Click the Open Perspective button in the panel in the upper-right corner of the window, then select Java Browsing Perspective. This perspective shows everything you need to easily create Java programs. When you create Java code, we'll look at additional Eclipse functionality for creating, modifying, and managing your code. But before that, you need to consider some basic concepts of object-oriented programming, which we will do in the next section. Let's now conclude this section with an overview of the interactive Java documentation.

Java API Online Help

 

Java's application programming interface (API) is very voluminous, so it's important to be able to find the information you need. The Java platform is quite large and provides you with almost any tool you need as a programmer. Learning how to use these features can require as much effort as learning the mechanisms of a programming language.

If you go to sun's Java documentation page (see Resources for a link), you'll see a link to the API documentation for each version of the SDK. Select version 1.4.2 to view the documentation.

You'll see three frames in your browser:

  • List of inline packages in the upper-left frame
  • List of all classes in the lower-left frame
  • Detailed information on the selected topic in the right frame

Every class in the SDK is present here. Select the HashMap class. On the right, you'll see a description of the class. At the top, you'll see the name and package in which it resides, its class hierarchy, the implemented interfaces (beyond the scope of this tutorial), and any direct subclasses it may have. After all this comes a description of the class. Sometimes the description includes a usage example, related references, style guidelines, etc. After the description, you will see a list of constructors, then a list of all the methods of the class, all inherited methods, and detailed descriptions of all methods. There is a lot of information, so there is a full pointer at the top and bottom of the right frame.

Many of the terms in the previous paragraph (such as package) are new to you. Don't bother. We will look at each of them in detail. For now, it's important to know that the Java documentation is available online.