Getting started with Java

This is some simple advice on how to run your first Java program. We'll start with the simplest scenario, running Java on the gl.umbc.edu linux machines using standard Unix tools, and then give some brief advice on setting up your own machine to run Java.

Running Java on gl.umbc.edu

Log into a linux machine in the gl environment. The java compiler is javac and the java interpreter is java. We'll start by compiling and running a hello world application, so create a directory like ~/java/HelloWorld/ and cd to it.

  % cd
  % mkdir java
  % cd java
  % mkdir HelloWorld
  % cd HelloWorld
Now copy the HelloWorld.java file into your directory. Compile the java source program, which will produce a java classfile HelloWorld.class
  % javac HelloWorld.java
  % ls -l
  total 2
  -rw------- 1 finin 41 426 Oct  8 13:01 HelloWorld.class
  -rw------- 1 finin 41 115 Oct  8 13:00 HelloWorld.java
Run your java program using the java command:
  % java HelloWorld
  Hello World!
  %

Fibonacci is another simple java program you can experiment with. COpy this into your directory. Compile it with javac and then run it with the java command. It prints out some initial numbers in the fibonacci series.

A third simple java program you can examine, compile and run is Factorial.java. This program takes an integer argument from the command line and prints the factorial of that number. Note that the conventions for accessing command lines arguments is different than those used in C and C++. In Java, the command line arguments are stored in an array args. The number of arguments can be found using the length method of an array. See the code for an example. Here's a session:

  % javac Factorial.java 
  % java Factorial 6
  720.0
  % java Factorial
  Usage: java Factorial 
  % java Factorial 6 6
  Usage: java Factorial 
  % java Factorial 100
  9.332621544394418E157
For a fourth example, consider a first Java assignment from a few years ago, computing Hailstone Sequences in Java, and a model solution. The solution directory has files making up a BlueJ project. The Hail.java file is the important one, of course.

Installing Java on your own computer

To use your own computer, you should install the following (free) software and documentation:

  • Java J2SE JDK. This is the Java 2 Standard Edition software development kit. Since you will be writing java programs, you need the software development kit (JDK) rather than just the Java runtime environment (JRE). Go to Sun's J2SE site to find the latest version of the J2SE JDK. When this page was last editied it was version 1.5 (http://java.sun.com/j2se/1.5.0/) Again: The JRE (Java Runtime Environment) is required to run Java programs; the JDK (Java Development Kit) is required to create Java programs. The JDK includes the JRE, so you don't need to download both.
  • J2SE 1.5 Documentation, same location as above. It's a good idea to have the document locally so you can access it when you are not online.
  • BlueJ is a very simple, free IDE (Integrated Development Environment) for Java. Get the current version from the BlueJ site at http://www.bluej.org. We will use BlueJ in this part of the course. You might want to go ahead and install it now, but be sure to install the Java JDK before you install BlueJ. See "Running BlueJ" for more information.

Note: The above software is available for Windows, Mac, and Linux, except that Java 1.5 is not yet available for MacOS X.

For more information