https://algs4.cs.princeton.edu/linux/
Hello World in Java on Linux
This DrJava-based Java programming environment is no longer being supported (because DrJava in no longer being actively developed and DrJava is incompatible with Java 11). It has been replaced by the following IntelliJ-based programming environment for Linux.
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).
- 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:~/]
~/
is shorthand for your home directory. - Install the Java Platform, Standard Edition Development Kit (JDK 8), either from Oracle or OpenJDK. Most Linux distributions provide their own mechanism for installing software. For example, here are the commands on Ubuntu for Oracle JDK 8:
[username:~/] sudo add-apt-repository ppa:webupd8team/java [username:~/] sudo apt-get update [username:~/] sudo apt-get install oracle-java8-installer [username:~/] sudo apt-get install oracle-java8-set-default
- 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.8.0_144
1.8
(or later) 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.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
1.8
(or later) for the Java version number, but the rest is not critical.
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.
- Create a directory
/usr/local/algs4
.[username:~/] cd /usr/local [username:/usr/local] sudo mkdir algs4 [username:/usr/local] sudo chmod 755 algs4
/usr/local/algs4
.[username:/usr/local] cd algs4 [username:/usr/local/algs4] pwd /usr/local/algs4
- Download the textbook libraries from algs4.jar and the Java wrapper scripts from javac-algs4, javac-cos226, javac-coursera, java-algs4, java-cos226, and java-coursera.
[username:/usr/local/algs4] sudo curl -O "https://algs4.cs.princeton.edu/code/algs4.jar" [username:/usr/local/algs4] sudo curl -O "https://algs4.cs.princeton.edu/linux/{javac,java}-{algs4,cos226,coursera}" [username:/usr/local/algs4] sudo chmod 755 {javac,java}-{algs4,cos226,coursera} [username:/usr/local/algs4] sudo mv {javac,java}-{algs4,cos226,coursera} /usr/local/bin
curl
downloads files from the web; if your system doesn't havecurl
, trywget
instead. - Download DrJava from drjava.jar, the wrapper script from drjava, and the configuration file from .drjava.
[username:/usr/local/algs4] sudo curl -O "https://algs4.cs.princeton.edu/linux/drjava.jar" [username:/usr/local/algs4] sudo curl -O "https://algs4.cs.princeton.edu/linux/drjava" [username:/usr/local/algs4] sudo chmod 755 drjava [username:/usr/local/algs4] sudo mv drjava /usr/local/bin [username:/usr/local/algs4] curl -O "https://algs4.cs.princeton.edu/linux/.drjava" [username:/usr/local/algs4] mv .drjava ~
- Download Findbugs 3.0.1 from findbugs.zip; our Findbugs configuration file from findbugs.xml; and the Findbugs wrapper scripts from findbugs-algs4, findbugs-cos226, and findbugs-coursera.
[username:/usr/local/algs4/] sudo curl -O "https://algs4.cs.princeton.edu/linux/findbugs.{zip,xml}" [username:/usr/local/algs4/] sudo curl -O "https://algs4.cs.princeton.edu/linux/findbugs-{algs4,cos226,coursera}" [username:/usr/local/algs4/] sudo unzip findbugs.zip [username:/usr/local/algs4/] sudo chmod 755 findbugs-{algs4,cos226,coursera} [username:/usr/local/algs4/] sudo mv findbugs-{algs4,cos226,coursera} /usr/local/bin
- Download PMD 5.8.1 from pmd.zip; our PMD configuration file from pmd.xml and the PMD wrapper scripts pmd-algs4, pmd-cos226, and pmd-coursera.
[username:/usr/local/algs4/] sudo curl -O "https://algs4.cs.princeton.edu/linux/pmd.{zip,xml}" [username:/usr/local/algs4/] sudo curl -O "https://algs4.cs.princeton.edu/linux/pmd-{algs4,cos226,coursera}" [username:/usr/local/algs4/] sudo unzip pmd.zip [username:/usr/local/algs4/] sudo chmod 755 pmd-{algs4,cos226,coursera} [username:/usr/local/algs4/] sudo mv pmd-{algs4,cos226,coursera} /usr/local/bin
- Download Checkstyle 8.2 from checkstyle.zip; our Checkstyle configuration files from checkstyle-algs4.xml, checkstyle-cos226.xml, and checkstyle-coursera.xml; and the Checkstyle wrapper scripts from checkstyle-algs4, checkstyle-cos226, and checkstyle-coursera.
[username:/usr/local/algs4/] sudo curl -O "https://algs4.cs.princeton.edu/linux/checkstyle.zip" [username:/usr/local/algs4/] sudo curl -O "https://algs4.cs.princeton.edu/linux/checkstyle-suppressions.xml" [username:/usr/local/algs4/] sudo curl -O "https://algs4.cs.princeton.edu/linux/checkstyle-{algs4,cos226,coursera}.xml" [username:/usr/local/algs4/] sudo curl -O "https://algs4.cs.princeton.edu/linux/checkstyle-{algs4,cos226,coursera}" [username:/usr/local/algs4/] sudo unzip checkstyle.zip [username:/usr/local/algs4/] sudo chmod 755 checkstyle-{algs4,cos226,coursera} [username:/usr/local/algs4/] sudo mv checkstyle-{algs4,cos226,coursera} /usr/local/bin
- For these wrapper scripts to work, it is important that
/usr/local/bin
is in yourPATH
environment variable. This is very likely the case. If not, see the Troubleshooting section below.
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.
- 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:~/] drjava
- If you copied the
.drjava
file to your home directory, these customizations should already be included:- 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:
/usr/local/algs4/algs4.jar
- Display line numbers by selecting Edit -> Preferences -> Display Options -> Show All Line Numbers.
- 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"); } }
- Finally, click the Save button to save the file in a directory of your choice. Name the file
HelloWorld.java
. The file name is case sensitive and must exactly match the name of the class in the Java program.
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.
- Type the following in the Interactions pane at the bottom. By convention, we highlight the text you type in bold.
> java HelloWorld
Welcome to DrJava. Working directory is /Users/username/algs4/hello > java HelloWorld Hello, World
- You may need to repeat this edit–compile–execute cycle a few times before it works.
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.
- From the shell, navigate to the directory containing
HelloWorld.java
, say~/hello
, by typing thecd
(change directory) commands below:[username:~/] cd hello [username:~/hello/]
- Compile
HelloWorld.java
by typing thejavac
command below:[username:~/hello/] javac HelloWorld.java [username:~/hello/]
HelloWorld.java
is in the current working directory, you should see no error messages. - To make our textbook libraries accessible to Java, use the command
javac-algs4
instead. For example, to compile TestAlgs4.java, which uses our standard drawing library, the auto-installer issues the the following command:machine:~/Desktop <username>$ javac-algs4 TestAlgs4.java
javac-cos226
andjavac-coursera
are equivalent tojavac-algs4
.
6. Execute a Program from the Shell |
You will use the java
command to execute your program.
- From the shell, type the
java
command below.[username:~/hello/] java HelloWorld Hello, World
- To make our textbook libraries accessible to Java, use the command
java-algs4
instead. For example, type the following two commands to test standard drawing and standard audio:[username:~/hello] java-algs4 edu.princeton.cs.algs4.StdDraw [ displays a graphics window with some geometric shapes and text ] [username:~/hello] java-algs4 edu.princeton.cs.algs4.StdAudio [ plays an A major scale ]
java-cos226
andjava-coursera
are equivalent tojava-algs4
.
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.
- To run Findbugs, type the following command in the Terminal:
[username:~/hello] findbugs-algs4 *.class Running findbugs on HelloWorld.class:
.class
files. Here is a list of bug descriptions. - To run PMD 5.8.1, type the following command in the Terminal:
[username:~/hello] pmd-algs4 .
.java
files. Here is a list of bug patterns. - To run Checkstyle, type the following command in the Terminal:
[username:~/hello] checkstyle-algs4 *.java Running checkstyle on HelloWorld.java: Starting audit... Audit done.
.java
files. Here is a list of available checks. - COS 226 students should use the following versions of these commands, to match the corresponding versions in the autograder:
[username:~/hello] findbugs-cos226 HelloWorld.class Running findbugs on HelloWorld.class: [username:~/hello] pmd-cos226 . [username:~/hello] checkstyle-cos226 HelloWorld.java Running checkstyle on HelloWorld.java: Starting audit... Audit done.
- Coursera students should use the following versions of these commands, to match the corresponding versions in the autograder:
[username:~/hello] findbugs-coursera HelloWorld.class Running findbugs on HelloWorld.class: [username:~/hello] pmd-coursera . [username:~/hello] checkstyle-coursera HelloWorld.java Running checkstyle on HelloWorld.java: Starting audit... Audit done.
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) should work fine. Note that DrJava is incompatible with Java 9, but everything else should be compatible with Java 9.
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
.
Last modified on August 14, 2019.