Hello World in Java on Linux
Beta version of instructions.
This document instructs you on how to setup our Java programming environment under Linux. It also provides a step-by-step guide for creating, compiling, and executing your first Java program using either DrJava or the command line. We assume some familiarity with the command line. All of the software is freely available on the web.
|
|
You will use the Java Platform, Standard Edition Development Kit (JDK 6)
and DrJava.
- Log in to the user account in which you will be programming. Your account must have Administrator privileges and you must be connected to the Internet.
- Launch your shell.
We'll assume that the command prompt looks like the following (those yours
will likely differ):
The ~/ is shorthand for your home directory.[username:~/]
- Create a directory ~/introcs and a subdirectory
~/introcs/bin.
[username:~/] mkdir introcs [username:~/] cd introcs [username:~/introcs/] mkdir bin
-
You will use Oracle's implementation of the Java Platform, Standard Edition Development
Kit (JDK 6). Most Linux distributions provide their own mechanism for
installing software. For example, on Gentoo, type:
If you use another distribution, use that distribution's package manager. (see the first Q+A under Troubleshooting). If it doesn't come with a package manager, install Java manually (see the second Q+A).[username:~/introcs/] sudo emerge --sync [username:~/introcs/] sudo emerge sun-jdk
- Download
DrJava from
drjava.jar
to ~/introcs.
The command wget downloads files from the web; if you don't have wget, use curl -O instead.[username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/drjava.jar [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/drjava [username:~/introcs/] chmod 700 drjava [username:~/introcs/] mv drjava bin
- Download the textbook libraries
stdlib.jar
and
algs4.jar
to ~/introcs.
[username:~/introcs/] wget http://introcs.cs.princeton.edu/java/stdlib/stdlib.jar [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/stdlib/algs4.jar
-
Download Checkstyle 5.4
and
Findbugs 1.3.9
from checkstyle.zip
and findbugs.zip to ~/introcs.
Unzip checkstyle.zip and findbugs.zip.
Downloads our checkstyle and findbugs configuration files
from checkstyle.xml and
findbugs.xml to ~/introcs.
Download the checkstyle and findbugs execution scripts
from checkstyle.sh
and findbugs.sh to ~/introcs/bin
and set the two files to be executable.
[username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/checkstyle.zip [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/findbugs.zip [username:~/introcs/] unzip checkstyle.zip [username:~/introcs/] unzip findbugs.zip [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/checkstyle.xml [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/findbugs.xml [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/checkstyle [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/findbugs [username:~/introcs/] chmod 700 checkstyle findbugs [username:~/introcs/] mv checkstyle findbugs bin [username:~/introcs/] mv checkstyle.xml checkstyle-5.4 [username:~/introcs/] mv findbugs.xml findbugs-1.3.9
-
Add the textbook standard libraries to the CLASSPATH environment variable
and add checkstyle and findbugs to your PATH environment variable.
The instructions depend on which shell you use.
- Bourne shell (sh), Bourne-again shell (bash), K-shell (ksh), or Z-shell (zsh).
Download config.sh to ~/introcs/bin.
Add the following line to the end of either ~/.bash_profile or ~/.profile, depending on which one exists:[username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/config.sh [username:~/introcs/] mv config.sh bin
test -r ~/introcs/bin/config.sh && source ~/introcs/bin/config.sh
- C-shell (csh) or T-shell (tcsh).
Download config.csh to ~/introcs/bin.
Add the following line to the end of either ~/.tcshrc or ~/.cshrc, depending on which one exists:[username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/config.csh [username:~/introcs/] mv config.csh bin
test -r ~/introcs/bin/config.csh && source ~/introcs/bin/config.csh
- Bourne shell (sh), Bourne-again shell (bash), K-shell (ksh), or Z-shell (zsh).
Download config.sh to ~/introcs/bin.
|
|
Now you are ready to write your first Java program.
You will develop your Java programs in an application called DrJava.
DrJava features many specialized programming tools including syntax highlighting,
bracket matching, auto indenting, and line numbering.
-
If you use a file manager such as Konqueror or Nautilus, you
can launch DrJava by double-clicking the drjava.jar file.
Otherwise, launch DrJava from the command line by typing:
[username:~/introcs/] drjava
- Make the following customizations.
- Display line numbers by selecting Edit -> Preferences -> Display Options -> Show All Line Numbers.
- Set the indentation level to 4 by selecting Edit -> Preferences -> Miscellaneous -> Indent Level -> 4.
-
Set the Java classpath by selecting
Edit -> Preferences -> Resources -> Extra Classpath -> Add
and adding the following two entries:
~/introcs/stdlib.jar
~/introcs/algs4.jar
-
In the main DrJava window, type the Java program
HelloWorld.java exactly as it appears below. If you omit even a semicolon,
the program won't work.
As you type, DrJava does the indenting for you.public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } - Finally, click the Save button to save the file. Use DrJava to create the directory ~/introcs/hello and name the file 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
execution on a computer. To do this, click the Compile button.
If all goes well, you should see the following message in the Compiler Output
pane at the bottom:
Compilation completed.
If DrJava complains in some way, you mistyped something. Check your program carefully, using the error messages in the Compiler Output pane as a guide.
|
|
Now it is time to run your program. This is the fun part.
-
Type the following in the Interactions pane at the bottom.
By convention, we highlight the text you type in bold.
If all goes well, you should see the following message:> java HelloWorld
Welcome to DrJava. Working directory is /Users/username/introcs/hello > java HelloWorld Hello, World
- You may need to repeat this edit-compile-execute cycle a few times before it works.
|
|
The command-line provides capabilities beyond those available in DrJava,
including redirection and piping.
You will type commands in an application called the shell.
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 Bourne-Again shell (bash), Bourne shell (sh), and C shell (csh).
-
To confirm that the Java compiler is installed,
type the command in boldface below and check that the results match:
It's important that you see the number 1.6 or 1.5 for the Java version number, but the rest is not critical.[username:~/] javac -version javac 1.6.0_26
-
To confirm that the Java interpreter is installed, type
the command in boldface below and check that the results match:
Again, it's important that you see the number 1.6 or 1.5 for the Java version number, but the rest is not critical.[username:~/] java -version java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511) Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)
|
|
You will use the javac command to convert your Java program into a form more amenable for execution on a computer.
-
From the shell, navigate to the directory containing HelloWorld.java,
say ~/introcs/hello,
by typing the cd (change directory) commands below:
[username:~/] cd introcs [username:~/introcs/] cd hello [username:~/introcs/hello/]
-
Compile it by typing the javac command below:
Assuming the file HelloWorld.java is in the current working directory, you should see no error messages.[username:~/introcs/hello/] javac HelloWorld.java [username:~/introcs/hello/]
|
|
You will use the java command to execute your program.
-
From the shell, type the java command below.
You should see the output of the program.[username:~/introcs/hello/] java HelloWorld Hello, World
|
|
You can use Checkstyle
and
Findbugs
to check the style of your programs and identify common bugs.
-
To run Checkstyle, type the following command in the Terminal:
Here is a list of available checks. You can customize the settings by editing the file /Users/username/introcs/checkstyle-5.4/checkstyle.xml.machine:~/introcs/hello username$ checkstyle HelloWorld.java Running checkstyle on HelloWorld.java: Starting audit... Audit done.
-
To run Findbugs, type the following command in the Terminal:
Here is a list of bug descriptions. You can customize the settings by editing the file /Users/username/introcs/findbugs-1.3.9/findbugs.xml.machine:~/introcs/hello username$ findbugs HelloWorld.class Running findbugs on HelloWorld.class:
|
|
My distribution of Linux is { Gentoo, Debian, Ubuntu, Fedora, Red Hat, SuSE, Mandriva, or Slackware }. How should I modify the instructions? We haven't tested out these instructions on all flavors of Linux, but the instructions should be identical except for installing Java. We recommend using your distribution's pacakge manager (such as portage, apt, or yum) to install Java.
How do I manually install Java?
- Download
Java SE 6 Update 27.
- Click on Download JDK button next to Java SE 6 Update 27. Be sure to choose the JDK, not the JRE. Also, be sure to choose Java SE 6, not Java SE 7. (You don't need the Java EE or NetBeans bundles.)
- Accept the Oracle Binary Code License Agreemment for Java SE.
- If you have root access, choose the Linux x64 - RPM Installer product by clicking the link jdk-6u27-linux-x64-rpm.bin; if not, choose the Linux x64 - Self Extracting Installer product by clicking the link jdk-6u27-linux-x64.bin.
-
Linux RPM installation.
Run the installation program by typing
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_6_0_18-linux-i586-rpm.bin [username:~/] rpm -iv jdk-1_6_0_18-linux-i586.rpm
-
Linux single user installation.
Run the installation program by typing
[username:~/] sh jdk-1_5_0_18-linux-i586.bin
Why Java 6 instead of Java 7? DrJava does not currently support Java 7.
Can I use a different version of Java? Yes, any version of Java 5 or Java 6 should work fine.
Can I update DrJava to use a newer version? Yes, but you do not need to do so.
I had to manually enter the location of tools.jar in DrJava, but it doesn't seem to have any effect. Any suggestions? This setting doesn't take effect until you restart DrJava.
Can I use a different IDE? Yes you can use another IDE (such as Eclipse) but you will have to configure the IDE properties yourself (such as the classpath).
How do I determine which shell I'm running? Type the following command:
You shell will likey be bash, tcsh, sh, ksh or zsh.
[username:~/] echo $SHELL bash
When I compile or execute a program from the shell that uses one of the textbook libraries, I get an error. How can I fix this? First, verify that you have the files ~/introcs/stdlib.jar and ~/introcs/algs4.jar. If so, it is an issue with the CLASSPATH environment variable. From the shell, type the following command to display it:
The CLASSPATH environment variable should include entries for ~/introcs/stdlib.jar and ~/introcs/algs4.jar.
[username:~/] echo $CLASSPATH
How do I break out of an infinite loop? From DrJava, click the Reset button in the menubar or select the menu option Tools -> Reset Interactions; from the shell, type Ctrl-c.
When using standard input, how do I signify that there is no more data?
If you are entering input from the keyboard, type Ctrl-d for EOF (end of file)
from either DrJava or the shell.
