Hello World in Java on Linux


Beta version of instructions (last updated 9/9/2017 to include PMD and the -cos226 and -coursera wrapper scripts).

This document instructs you on how to setup a 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 Java


You will use the Java Platform, Standard Edition Development Kit (JDK 8).


1.   Install a Programming Environment


These steps install and configures our textbook libraries, DrJava, Findbugs, PMD, an Checkstyle. If you do not want DrJava, Findbugs, PMD, or Checkstyle, simply skip the corresponding step.


2.   Create a 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.


3.   Compile a 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.


4.   Execute a Program from DrJava


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


5.   Compile a 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.


6.   Execute a Program from the Shell


You will use the java command to execute your program.


7.   Static Code Analysis Tools


You can use Findbugs, PMD, and Checkstyle and to check the style of your programs and identify common bug patterns.


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.

Can I use a different version of Java? Yes. Any version of Java 8 (either Oracle or OpenJDK) or later should work fine.

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.

How can I check that /usr/local/bin is in my PATH? From the shell, type the following command:

[username:~/] echo $PATH
You should see an entry /usr/local/bin, with entries separated by colon (:) characters.

How can I add /usr/local/bin to my PATH? It 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. For other common shells, this could mean updating your .cshrc, .tcshrc, or .zcshrc files.

# Add ~/algs4/bin to the PATH
export PATH=$PATH:$HOME/algs4/bin
You may need to logout and log back in for it to take effect.

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 file /usr/local/algs4/algs4.jar.