# 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.

 0.   Install the Programming Environment

You will use the Java Platform, Standard Edition Development Kit (JDK 7) 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 (though yours will likely differ):
 [username:~/] 
The ~/ is shorthand for your home directory.

• Create a directory ~/introcs and a subdirectory ~/introcs/bin.
 [username:~/] mkdir introcs [username:~/] cd introcs [username:~/introcs/] mkdir bin 

• You will use either Oracle's implementation of the Java Platform, Standard Edition Development Kit (JDK 7) or the open-source implementation OpenJDK. Most Linux distributions provide their own mechanism for installing software. For example, on Ubuntu or Debian, type:
 [username:~/introcs/] sudo apt-get update [username:~/introcs/] sudo apt-get install openjdk-7-jdk 
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).

• Download DrJava and the wrapper script from drjava.jar and drjava to the directory ~/introcs/.
 [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 
The command wget downloads files from the web; if you don't have wget, try curl -O instead.

• Download the textbook standard libraries stdlib.jar to ~/introcs. Download the Java wrapper scripts from javac-introcs and java-introcs to ~/introcs/bin and set the two files to be executable.
 [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/stdlib/stdlib.jar [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/javac-introcs [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/java-introcs [username:~/introcs/] chmod 700 javac-introcs java-introcs [username:~/introcs/] mv javac-introcs bin [username:~/introcs/] mv java-introcs bin 

• Download Checkstyle 5.5 and Findbugs 2.0.3 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-introcs and findbugs-introcs 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-introcs [username:~/introcs/] wget http://introcs.cs.princeton.edu/java/linux/findbugs-introcs [username:~/introcs/] chmod 700 checkstyle-introcs findbugs-introcs [username:~/introcs/] mv checkstyle-introcs bin [username:~/introcs/] mv findbugs-introcs bin [username:~/introcs/] mv checkstyle.xml checkstyle-5.5 [username:~/introcs/] mv findbugs.xml findbugs-2.0.3 

• Add the directory ~/introcs/bin to your PATH environment variable. This will depends significantly on your Linux distribution, shell, and user configuration. In the bash shell, this generally means adding the following line to either your .bash_profile, .profile, or .bashrc file.
 # Add ~/introcs/bin to the PATH export PATH=$PATH:$HOME/introcs/bin 
This is a critical step. You may need to logout and log back in for it to take effect.

 1.   Create the Program in DrJava

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 add the following entry:
~/introcs/stdlib.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.
 public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); } } 
As you type, DrJava does the indenting for you.

• 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.

 2.   Compile the Program from DrJava

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.

 3.   Execute the Program from DrJava

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.
 > java HelloWorld 
If all goes well, you should see the following message:
 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.

 4.   Command-Line Interface

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:
 [username:~/] javac -version javac 1.7.0_51 
It's important that you see the number 1.7 (or 1.6 or 1.8) for the Java version number, but the rest is not critical.

• To confirm that the Java interpreter is installed, type the command in boldface below and check that the results match:
 [username:~/] java -version java version "1.7.0_51" Java(TM) SE Runtime Environment (build 1.7.0_51-b03-383-11A511) Java HotSpot(TM) 64-Bit Server VM (build 20.1-b03-383, mixed mode) 
Again, it's important that you see the number 1.7 (or 1.6 or 1.8) for the Java version number, but the rest is not critical.

 5.   Compile the Program from the Shell

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:
 [username:~/introcs/hello/] javac HelloWorld.java [username:~/introcs/hello/] 
Assuming the file HelloWorld.java is in the current working directory, you should see no error messages.

• If you want to classpath in our standard libraries, use the command javac-introcs instead.

 6.   Execute the Program from the Shell

You will use the java command to execute your program.

• From the shell, type the java command below.
 [username:~/introcs/hello/] java HelloWorld Hello, World 
You should see the output of the program.
• If you want to classpath in our standard libraries, use the command java-introcs instead. For example, to test standard draw and standard audio type the following two commands:
 machine:~/introcs/hello username$java-introcs StdDraw [ displays a graphics window with some geometric shapes and text ] machine:~/introcs/hello username$ java-introcs StdAudio [ plays an A major scale ] 

 7.   Checkstyle and Findbugs

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:
 machine:~/introcs/hello username$checkstyle-introcs HelloWorld.java Running checkstyle on HelloWorld.java: Starting audit... Audit done.  Here is a list of available checks. You can customize the settings by editing the file /Users/username/introcs/checkstyle-5.5/checkstyle.xml. • To run Findbugs, type the following command in the Terminal:  machine:~/introcs/hello username$ findbugs-introcs HelloWorld.class Running findbugs on HelloWorld.class: 
Here is a list of bug descriptions. You can customize the settings by editing the file /Users/username/introcs/findbugs-2.0.3/findbugs.xml.

 Troubleshooting

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 package manager (such as portage, apt, emerge, or yum) to install Java. Here are some instructions for installing OpenJDK.

How do I manually install Java? Download and install of Oracle's implementation of the Java Platform, Standard Edition Development Kit (JDK 7) by clicking the button next to Java SE 7u51 and following the instructions.

Can I use a different version of Java? Yes, any version of Java 6 or Java 7 should work fine. (You can also use Java 8, but you will need to upgrade to a newer version of Findbugs.)

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, feel free to 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:

 [username:~/] echo $SHELL bash  You shell will likey be bash, tcsh, sh, ksh or zsh. 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, make sure that you are using the javac-algs4 and java-algs4 wrapper scripts. Next, verify that you have the files ~/algs4/stdlib.jar and ~/algs4/algs4.jar. If so, it is probably an issue with the PATH environment variable. From the shell, type the following command to display it:  [username:~/] echo$PATH 
The PATH environment variable should include an entry for ~/algs4/bin.

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.