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.
|
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:~/]
-
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
-
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)
|
These steps install and configures our textbook libraries, DrJava, Checkstyle, and Findbugs.
If you do not want DrJava, Checkstyle, or Findbugs, simply skip the corresponding step.
- Create a directory /usr/local/introcs.
[username:~/] cd /usr/local [username:/usr/local] sudo mkdir introcs [username:/usr/local] sudo chmod 755 introcs
[username:/usr/local] cd introcs [username:/usr/local/introcs] pwd /usr/local/introcs
- Download the textbook libraries from
stdlib.jar
and the Java wrapper scripts from javac-introcs and java-introcs.
[username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/code/stdlib.jar [username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/javac-introcs [username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/java-introcs [username:/usr/local/introcs] sudo chmod 755 javac-introcs java-introcs [username:/usr/local/introcs] sudo mv javac-introcs /usr/local/bin [username:/usr/local/introcs] sudo mv java-introcs /usr/local/bin
- Download
DrJava
from
drjava.jar,
the wrapper script from
drjava,
and the configuration file from
.drjava.
[username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/drjava.jar [username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/drjava [username:/usr/local/introcs] sudo wget http://introcs.cs.princeton.edu/java/linux/.drjava [username:/usr/local/introcs] sudo chmod 755 drjava [username:/usr/local/introcs] sudo mv drjava /usr/local/bin [username:/usr/local/introcs] sudo mv .drjava ~
-
Download Checkstyle 8.2
from checkstyle.zip;
our Checkstyle configuration file
from checkstyle-introcs.xml;
and the Checkstyle wrapper script from
from checkstyle-introcs
[username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/checkstyle.zip [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/checkstyle-introcs.xml [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/checkstyle-suppressions.xml [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/checkstyle-introcs [username:/usr/local/introcs/] sudo unzip checkstyle.zip [username:/usr/local/introcs/] sudo chmod 755 checkstyle-introcs [username:/usr/local/introcs/] sudo mv checkstyle-introcs /usr/local/bin
-
Download
Findbugs 3.0.1
from findbugs.zip;
our Findbugs configuration file
from findbugs.xml;
and the Findbugs wrapper script from
findbugs-introcs.
[username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/findbugs.zip [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/findbugs.xml [username:/usr/local/introcs/] sudo wget http://introcs.cs.princeton.edu/java/linux/findbugs-introcs [username:/usr/local/introcs/] sudo unzip findbugs.zip [username:/usr/local/introcs/] sudo chmod 755 findbugs-introcs [username:/usr/local/introcs/] sudo mv findbugs-introcs /usr/local/bin
- For these wrapper scripts to work, it is important that /usr/local/bin is in your PATH environment variable. This is very likely the case. If not, see the Troubleshooting section below.
|
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/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"); } }
- 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.
|
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.
> 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.
|
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 the cd (change directory) commands below:
[username:~/] cd hello [username:~/hello/]
-
Compile it by typing the javac command below:
[username:~/hello/] javac HelloWorld.java [username:~/hello/]
- To make our textbook libraries accessible to Java, use the command javac-introcs instead.
|
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-introcs instead.
For example, type the following two commands to test standard drawing and standard audio:
machine:~/hello username$ java-introcs StdDraw [ displays a graphics window with some geometric shapes and text ] machine:~/hello username$ java-introcs StdAudio [ plays an A major scale ]
|
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:~/hello username$ checkstyle-introcs HelloWorld.java Running checkstyle on HelloWorld.java: Starting audit... Audit done.
-
To run Findbugs, type the following command in the Terminal:
machine:~/hello username$ findbugs-introcs 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 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? 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:
You shell will likey be bash, tcsh, sh, ksh or zsh.
[username:~/] echo $SHELL bash
How can I check that /usr/local/bin is in my PATH? From the shell, type the following command:
You should see an entry /usr/local/bin, with entries separated by colon (:) characters.
[username:~/] echo $PATH
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.
You may need to logout and log back in for it to take effect.
# Add ~/introcs/bin to the PATH export PATH=$PATH:$HOME/introcs/bin
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-introcs and java-introcs
wrapper scripts.
Next, verify that you have the file /usr/local/introcs/stdlib.jar.