Hello, World in Java on Linux
This document instructs you on how to setup a Java programming
environment under the Debian flavor of Linux, and provides a
step-by-step guide to creating, compiling, and executing a Java
program. Red Hat, SuSE, Mandrake, Debian, Gentoo,
and other Linux distros should be similar.
All of the software is freely available on the web.
You will use the Java compiler javac to compile your Java programs and
the Java interpreter java to run them.
-
Download the
Java 2 standard edition J2SE SDK v 1.4.2.
Be sure to choose SDK, not the JRE. You don't need the NetBeans IDE cobundle.
Download either the Linux RPM in self-extracting file
(if you have root access) or
the Linux self-extracting file (if you don't have root access
and want to install for a single user).
- Install Java by opening up a shell window and enter the following
commands into the shell, depending on whether you chose the RPM or single-user installation
option.
While it's installing, you might enjoy reading Neal Stephenson's light-hearted
essay In the Beginning
was the Command Line.
-
Linux RPM installation.
Run the installation program by typing
chmod +x j2sdk-1_4_2-linux-i586-rpm.bin
./j2sdk-1_4_2-linux-i586-rpm.bin
rpm -iv j2sdk-1_4_2-linux-i586.rpm
If dependencies are missing and the RPM package cannot install, you either
need to download the dependencies or use the single-user installation.
-
Linux single user installation.
Run the installation program by typing
chmod +x j2sdk-1_4_2-linux-i586.bin
./j2sdk-1_4_2-linux-i586.bin
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!).
-
To configure Java, you will need to know which shell you are running.
In case you don't know, type the following command:
finger username
but replace username with your username.
Your shell will likely be bash, tcsh, sh,
or ksh.
-
To make sure Linux can find the Java compiler and interpreter,
edit your shell login file according to the shell you are using.
- Bash.
If you are using the
bash shell,
append the following line to your .bashrc file.
If it doesn't exist, create a new file.
export PATH=$PATH:~/j2sdk1.4.2/bin
Then close your shell and open up a new one.
If it does not work, in addition, try repeating the same instructions
with your .bashrc file.
- Tcsh.
If you are using the tcsh shell, append the following line to your
.tcshrc file:
setenv PATH ~/j2sdk1.4.2/bin:$PATH
Then close your shell and open up a new one.
-
Now, create a directory to store your Java programs.
At the command line, type the bold commands below:
[bzankel@silver:~/] mkdir introcs
[bzankel@silver:~/] cd introcs/
[bzankel@silver:~/] mkdir hello
[bzankel@silver:~/introcs] cd hello/
[bzankel@silver:~/introcs/hello]
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 ~/introcs/hello. All of your
files for Assignment 0 will go here.
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.
You will type and edit your programs in a text editor called JEdit.
JEdit is similar to conventional word processors like MS Word and Notepad,
but it features many specialized programming tools including
syntax highlighting, bracket matching, auto indenting, indent
shifting, line numbering, and commenting out code. It's even written in Java.
-
Download and install
the latest stable version of JEdit using the Linux RPM package
if you have root access or the
Java-based installer if you don't. Install it
using the default options.
-
We recommend the following JEdit customizations.
- Change the default indentation to four spaces via
Utilities -> Global Options -> Editing -> Indent Width and
Utilities -> Global Options -> Editing -> Tab Width.
- Change the default tab width when printing to four spaces via
Utilities -> Global Options -> Printing -> Tab width when printing
- Add line numbers by checking Utilities -> Global Options -> Gutter -> Line Numbering
- Allow at most 80 characters per line by
Utilities -> Global Options -> Editing -> Word wrap -> hard and
Utilities -> Global Options -> Editing -> Wrap margin -> 80.
- Change the default line separator to Unix via
Utilities -> Global Options -> Loading & Saving -> Default Line Separator
- Remove the annoying . that marks the end of a line by unchecking
Utilities -> Global Options -> Text Area -> End of Line Markers
Now you are ready to write your first Java program.
-
Launch JEdit.
-
In the JEdit window, type the Java program exactly as it appears below.
If you omit even a semicolon, the program won't work.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
-
When you're done, save the program using
File -> Save As, chose the directory ~/introcs/hello
and type in the file name HelloWorld.java.
The file name is case sensitive and must exactly match the name of the class
in the Java program.
It is now time to convert your Java program into a form more amenable for executing
on a computer.
-
From the command line, type
[bzankel@silver:~/introcs/hello] javac HelloWorld.java
If javac complains in some way, you mistyped something, and you should
check your program carefully. Ask for help if you can't see the mistake.
- If everything went well, you should see the following
in the command line:
[bzankel@silver:~/introcs/hello] javac HelloWorld.java
[bzankel@silver:~/introcs/hello]
Silence is golden in computer science.
Now it is time to run your program. This is the fun part.
-
At the command line, type
[bzankel@silver:~/introcs/hello] java HelloWorld
-
If all goes well, you should see
[bzankel@silver:~/introcs/hello] java HelloWorld
Hello, World
- You may need to repeat this
edit-compile-execute cycle a few times before it all goes smoothly.
Use the up and down arrow keys to repeat previous commands and avoid extra typing.
Congratulations, you are now a Java programmer!
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.