Hello, World in Java 1.5 on Linux


This document instructs you on how to setup a Java programming environment under the Gentoo flavor of Linux, and provides a step-by-step guide for creating, compiling, and executing a Java program. Other Linux distributions (e.g., Ubuntu, Fedora, Red Hat, SuSE, Mandriva, Gentoo, Slackware, etc.) should be similar. All of the software is freely available on the web.

 

Java

You will use Sun's implementation of Java 2 Standard Edition (J2SE) 5.0. Most Linux distributions provide their own mechanism for installing software. For example, on Gentoo, type

[username:~/] sudo emerge --sync
[username:~/] sudo emerge sun-jdk
If you use another distribution, consult the documentation for instructions on how to install it. If it doesn't come with a package manager, see the first Q+A under troubleshooting.

 

Command Line Interface

You will type commands in an application known as the shell. Since you're using Linux, we assume you're somewhat familiar with it (as you used it in the previous step!).

If you plan to take COS 217, you might want to buy the required book Programming with GNU Software by Loukides and Oram. It contains an overview of Unix from the user's point of view. It also describes shell fundamentals, with reference to the bash, Bourne, and C shells.

 

Create the program

Now you are ready to write your first Java program.

 

Compile the program

It is now time to convert your Java program into a form more amenable for execution on a computer. To do this, enter the following command at your shell prompt:

[username:~/introcs/hello] javac HelloWorld.java

If the Java compiler complains in some way, you mistyped something, and you should check your program carefully. Use the error messages to guide your search.

 

Execute the program

Now it is time to run your program. This is the fun part.

Congratulations, you are now a Java programmer!

 

Troubleshooting

My Linux distribution doesn't come with a package manager. Can I install Java manually? Yes, download the Java 2 Platform Standard Edition 5.0 to the Desktop:

How can I check which version of Java I'm using? Type the following two commands.

[username:~/] java -version
java version "1.5.0_14"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_14-112)
Java HotSpot(TM) Client VM (build 1.5.0_14-64, mixed mode, sharing)

[username:~/] javac -version
javac 1.5.0_14
javac: no source files

When I try to run java I get: Exception in thread "main" java.lang.NoClassDefFoundError. First, be sure that HelloWorld.class is now in the current directory. Be sure to type java HelloWorld without a trailing .class or .java. If this was not your problem, it's possible that your CLASSPATH was set by some other program so that it no longer includes the current working directory. Try running your program with the command line

[username:~/introcs/hello] java -classpath ./ HelloWorld

How do I permanently set the CLASSPATH? Put the following line in your .bashrc file (or an analogous line if you are using another shell).

export CLASSPATH=.:/path/to/look/in:$CLASSPATH
To check your CLASSPATH variable, type echo $CLASSPATH.

How do I set the path with the tcsh shell? Append the following line to your ~username/.tcshrc file:

setenv PATH ~/j2sdk1.5.0.14/bin:$PATH

How do I configure vi to replace tabs with four spaces? Edit your .exrc file and add the commands ":set ts=4" to display the tabs as four spaces and ":set expandtab" to translate the tabs into spaces.