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.


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.


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.


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


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.


6.   Execute the 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.

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.