Prepare your first cup of Java on Unix

Java is an operating system-independent platform for software development. It consists of a programming language, utility programs, and a runtime environment. A Java program can be developed on one computer and run on any other computer with the correct runtime environment. In general, older Java programs can run in newer runtime 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 popularity, because when programming for the web, there is no way of knowing what 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, it does not generate instructions for a specific platform. Instead, Java bytecode is generated, that is, instructions for the Java Virtual Machine (Java VM). For users, it doesn’t matter what platform they use – Windows, Unix, MacOS, or an Internet browser – as long as you have the Java VM, it understands those byte codes.

Three types of Java programs

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

In both of these cases, the Java program cannot run without the services of a web browser for an applet or a web server for a servlet.

  • A Java application is a Java program that can run itself.

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

a checklist

Very simple, only two elements are needed 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 work (eg Vi, Emacs, Pico). We will 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) to your computer. This way you can save some typing. Then you can go directly to step 2.

Or you can follow the longer instructions:

(1) Open a shell window (sometimes called a terminal).

When the prompt appears, your current directory will normally 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 that you create must be saved in a separate directory. You can create a directory using the mkdir command . For example, to create the java directory in your home directory, you would first change your current directory to your home directory by entering the following command:

 %dc 

So, you would enter the following command:

 % mkdir java 

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

Now you can start creating your source file.

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

When you start Pico, it will display a new blank buffer. This is the area where you will write your code.

(3) Enter in the blank buffer the code that appears at the end of this document (under “Sample Java Program”). 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 where you want the file to go. If you want to save FatCalories.java in the /home/smith/java directory, 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 in a bytecode file.

Now bring up another 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, type the following command at the command line 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 type ls at the prompt, you should see your file: FatCalories.java.

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

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

so Unix can’t find the Java compiler, javac.

Here’s a way to tell Unix where to find Java. Assume that you have installed the Java 2 Platform (J2SE) in /usr/java/jdk1.4. At the command line, type the following command and press Return:

/usr/java/jdk1.4/javacFatCalories.java

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

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

Step 3. Run the program

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

In the same directory, enter when prompted:java FatCalories

When you run the program, you will need to enter two numbers when the black command line window appears. The program must then write those two numbers plus the percentage calculated by the program.

When you get the error message:

Exception in “main” thread java.lang.NoClassDefFoundError: FatCalories

This means: java cannot find your bytecode file, FatCalories.class.

What to do: One of the places where java tries to find your bytecode file is your current directory. For example, if your bytecode file is in /home/smith/java, you would change your current directory to that directory by typing the following command at the command line and pressing Return:

 cd /home/smith/java 

If you enter pwd at the prompt, you should see /home/smith/java. If you type 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 may need to change your CLASSPATH variable. To see if this is necessary, try “unsetting” the classpath with the following command:

Unset CLASSPATH

 unset CLASSPATH 

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

TechnoAdmin