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
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.[username:~/] sudo emerge --sync [username:~/] sudo emerge sun-jdk
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!).
Your shell will likely be bash, tcsh, sh, or ksh.[username:~/] echo $SHELL
Then close your shell and open up a new one. If it does not work, in addition, try repeating the same instructions with the file ~username/.bash_profile.export PATH=$PATH:~/j2sdk1.5.0.14/bin
The mkdir command creates a new directory; the cd command changes the current working directory. After executing these commands, your working directory is the newly created ~username/introcs/hello. All of your files for Assignment 0 will go here.[username:~/] mkdir introcs [username:~/] cd introcs [username:~/] mkdir hello [username:~/introcs] cd hello [username:~/introcs/hello]
Now you are ready to write your first Java program.
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } }
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.
Now it is time to run your program. This is the fun part.
[username:~/introcs/hello] java HelloWorld Hello, World!
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:
If dependencies are missing and the RPM package cannot install, you either need to download the dependencies or use the single-user installation.[username:~/] sh jdk-1_5_0_14-linux-i586-rpm.bin [username:~/] rpm -iv jdk-1_5_0_14-linux-i586.rpm
[username:~/] sh jdk-1_5_0_14-linux-i586.bin
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).
To check your CLASSPATH variable, type echo $CLASSPATH.export CLASSPATH=.:/path/to/look/in:$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.