Brew Your First Cup of Java on Unix

Instructions for programming a simple Java application on Unix

cup of java
public domain

Great Things About Java

Java is an operating system independent platform for software development. It consists of a programming language, utility programs and a run time environment. A Java program can be developed on one computer and run on any other computer with the correct run time environment. In general, older Java programs can run on newer run time environments. Java is rich enough that even very complicated applications can be written without operating system dependencies.

This is called 100% Java.

With the development of the internet Java has gained in popularity, because when you program for the Web, you have no way of knowing which system the user may be on. With the Java programming language, you can take advantage of the "write once, run anywhere" paradigm. This means that when you compile your Java program, you don't generate instructions for one specific platform. Instead, you generate Java byte code, that is, instructions for the Java Virtual Machine (Java VM). For the users, it doesn’t matter what platform they use--Windows, Unix, MacOS, or an Internet browser—as long as it has the Java VM, it understands those byte codes.

Three Types of Java Programs

- An "applet" is a Java program designed to be embedded on a web page.
- A "servlet" is a Java program designed to be run on a server.

In these two cases the Java program cannot be run without the services of either a Web browser for an applet or a Web server for a servlet.

- A "Java application" is a Java program that can be run by itself.

The following instructions are for you to program a Java application using a Unix-based computer.

A Checklist

Very simple, you need only two items to write a Java program:

(1) The Java 2 Platform, Standard Edition (J2SE), formerly known as the Java Development Kit (JDK).


Download the latest version for Linux. Make sure you download the SDK, not the JRE (the JRE is included in the SDK/J2SE).

(2) A text editor
Almost any editor you find on Unix-based platforms will do (e.g., Vi, Emacs, Pico). We’ll use Pico as an example.

Step 1. Create a Java Source File.

A source file contains text written in the Java programming language. You can use any text editor to create and edit source files.

You have two options:

* You can save the FatCalories.java file (at the end of this article) onto your computer. This way can save you some typing. Then, you can go straight to step 2.

* Or, you can follow the longer instructions:

(1) Bring up a shell (sometimes called terminal) window.

When the prompt first comes up, your current directory will usually be your home directory. You can change your current directory to your home directory at any time by typing cd at the prompt (typically a “%”) and then pressing Return.

The Java files you create should be kept in a separate directory. You can create a directory by using the command mkdir. For example, to create the directory java in your home directory, you would first change your current directory to your home directory by entering the following command:
% cd

Then, you would enter the following command:
% mkdir java

To change your current directory to this new directory, you would then enter: % cd java

Now you can start creating your source file.

(2) Start the Pico editor by typing pico at the prompt and pressing Return. If the system responds with the message pico: command not found, then Pico is most likely unavailable. Consult your system administrator for more information, or use another editor.

When you start Pico, it'll display a new, blank buffer. This is the area in which you will type your code.

(3) Type the code listed at the end of this article (under “Sample Java Program”) into the blank buffer. Type everything exactly as shown. The Java compiler and interpreter are case-sensitive.

(4) Save the code by typing Ctrl-O.

When you see File Name to write:, type FatCalories.java, preceded by the directory in which you want the file to go. If you wish to save FatCalories.java in the directory /home/smith/java, then you would type

/home/smith/java/FatCalories.java and press Return.

Use Ctrl-X to exit Pico.

Step 2. Compile the Source File.

The Java compiler, javac, takes your source file and translates its text into instructions that the Java Virtual Machine (Java VM) can understand. The compiler puts these instructions into a byte code file.

Now, bring up another shell window. To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is /home/smith/java, you would type the following command at the prompt and press Return:
% cd /home/smith/java

If you enter pwd at the prompt, you should see the current directory, which in this example has been changed to /home/smith/java.

If you enter ls at the prompt, you should see your file: FatCalories.java.

Now you can compile. At the prompt, type the following command and press Return: javac FatCalories.java

If you see this error message:
javac: Command not found

then Unix cannot find the Java compiler, javac.

Here's one way to tell Unix where to find javac. Suppose you installed the Java 2 Platform (J2SE) in /usr/java/jdk1.4. At the prompt, type the following command and press Return:

/usr/java/jdk1.4/javac FatCalories.java

The compiler now has generated a Java byte code file: FatCalories.class.

At the prompt, type ls to verify the new file is there.

Step 3. Run the Program

The Java VM is implemented by a Java interpreter called java. This interpreter takes your byte code file and carries out the instructions by translating them into instructions that your computer can understand.

In the same directory, enter at the prompt:
java FatCalories

When you run the program you need to enter two numbers when the black command line window appears. The program should then write out those two numbers plus the percentage computed by the program.

When you receive the error message:

Exception in thread "main" java.lang.NoClassDefFoundError: FatCalories

It means: java cannot find your byte code file, FatCalories.class.

What to do: One of the places java tries to find your byte code file is your current directory. For example, if your byte code file is in /home/smith/java, you should change your current directory to that by typing the following command at the prompt and hit Return:

cd /home/smith/java

If you enter pwd at the prompt, you should see /home/smith/java. If you enter ls at the prompt, you should see your FatCalories.java and FatCalories.class files. Now enter java FatCalories again.

If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try "unsetting" the classpath with the following command:

unset CLASSPATH

Now enter java FatCalories again. If the program works now, you'll have to change your CLASSPATH variable.

Was this page helpful?