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


1.   Install a Programming Environment


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.


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.   Checkstyle and Findbugs


You can use Checkstyle and Findbugs to check the style of your programs and identify common bugs.


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? 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 ~/introcs/bin to the PATH
export PATH=$PATH:$HOME/introcs/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-introcs and java-introcs wrapper scripts. Next, verify that you have the file /usr/local/introcs/stdlib.jar.