This document instructs you on how to set up a Java programming environment
for your Linux
computer.
It also provides a step-by-step guide for creating and compiling a Java
program in IntelliJ and executing it from the command line.
You will need a 64-bit version of Linux (such as Ubuntu, Fedora, or Debian) with root privileges.
1. Install Java |
You will use
Java SE Development Kit 11 (JDK 11).
Note
Skip this step if you already have JDK 11 installed.
~>
The symbol ~
is shorthand for your home directory.
~> sudo apt-get update ~> sudo apt-get install openjdk-11-jdk
~> javac -version javac 11.0.24 ~> java -version openjdk version "11.0.24" 2024-07-16 OpenJDK Runtime Environment (build 11.0.24+8) OpenJDK 64-Bit Server VM (build 11.0.24+8, mixed mode, sharing)It’s important that the Java version numbers match and that you see the number
11
,
but the rest is not critical.
2. Install Command-Line Tools |
Next, you will install our textbook libraries,
SpotBugs, PMD, and Checkstyle
to
/usr/local/lift
and associated wrapper scripts to
/usr/local/bin
.
~> cd /usr/local /usr/local/> sudo curl -O "https://lift.cs.princeton.edu/java/linux/lift-cli.zip" /usr/local/> sudo unzip lift-cli.zip /usr/local/> sudo rm lift-cli.zipThe command
curl
downloads files from the web.
~> java-introcs StdAudioYou should hear a sequence of sounds (concert A, helicopter blades, a voice saying the number twenty-four, a Mozart measure, and a simple drum beat.
.bashrc
) or set any
environment variables (such as JAVA_HOME
or CLASSPATH
).
For reference, here are our recommended shell configuration files for Bash:
3. Install IntelliJ |
Now, you will install IntelliJ.
Warning
Be sure to choose the Community Edition and 2024.2. Our customized IntelliJ preferences won't be active if you choose the Ultimate Edition or 2024.3.
~> cd ~> rm -rf .cache/JetBrains/IdeaIC2024.2 ~> rm -rf .config/JetBrains/IdeaIC2024.2 ~> rm -rf .local/share/JetBrains/IdeaIC2024.2 ~> curl -O "https://lift.cs.princeton.edu/java/linux/IdeaIC2024.2.zip" ~> unzip IdeaIC2024.2.zip ~> rm IdeaIC2024.2.zip
Warning
This will overwrite any previous IntelliJ 2024.2 settings with our novice-friendly settings.
4. Open a Project in IntelliJ |
You will develop your Java programs in an application called IntelliJ IDEA, Community Edition.
IntelliJ organizes Java programs into projects. In our context, each project corresponds to one programming assignment. A typical project contains Java programs, associated data files, and course-specific settings (such as compiler options, style rules, and textbook libraries).
[ sample project for COS 126 (Princeton) ]
[ sample project for COS 226 (Princeton) ]
[ sample project for Computer Science: Programming with a Purpose (Coursera) ]
[ sample project for Algorithms, Part I or II (Coursera) ]
Unzip the zip file using the following command:
~> unzip -d hello hello.zipThis creates a project folder with the name of the corresponding programming assignment (such as
hello
).
Delete the zip file.
Warning
The project folders contain course-specific information. Be sure to download the one corresponding to your institution and course.
You should see an assignment logo (in the main editor window) and a list of
project files (in the Project View sidebar at left).
If you don't see the Project View sidebar,
select LIFT → Project (Ctrl + 1) to toggle it on.
Warning
Do not click New Project; this option is intended for advanced programmers. Also, always use Open with a project folder, not an individual file./usr/lib/jvm/java-11-openjdk-amd64/
.
5. Create a Program in IntelliJ |
Now you are ready to write your first Java program.
IntelliJ features many specialized programming tools
including line numbering, syntax highlighting, bracket matching, auto indenting,
auto formatting, auto importing, variable renaming, and continuous code inspection.
HelloWorld.java
exactly as it appears below.
Note that IntelliJ generates the gray boilerplate code automatically. 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"); } }
Tip
IntelliJ is configured to automatically save changes that you make to your files upon various events (such as compiling, executing, closing a file or project, or quitting the IDE). We still recommend using File → Save All (Ctrl + S) frequently for its code reformatting functionality.
6. Compile and Execute the Program (from IntelliJ) |
Now, it is time to execute (or run) your program.
This is the exciting part, where your computer follows the instructions
specified by your program.
Before doing so, you must compile your program into
a form more amenable for execution on a computer.
If the compilation fails, a Recompile panel will open up (at bottom), highlighting the compile-time errors or warnings. Check your program carefully for typos, using the error messages as a guide.
You should see the output of the program (in white), along with a message that the program finished normally (with exit code 0).
Tip
Use the LIFT menu to compile and execute your program from IntelliJ. The Build and Run menus support additional options for advanced programmers.
Also be sure that the main editor window is active before using the LIFT menu (e.g., by clicking the code you want to compile or execute).
7. Compile and Execute the Program (from the command line) |
The command line is a simple and powerful mechanism for
controlling your programs (e.g., command-line arguments,
file redirection, and piping).
IntelliJ supplies an embedded terminal
for easy access to the command line.
~/hello>
The ~/hello
is the current working
directory, where
~
is shorthand for your home directory.
javac
command.
More specifically, type the text in yellow that appears on the same line as the
command prompt.
~/hello> javac HelloWorld.java ~/hello>Assuming that the file
HelloWorld.java
is in the current working directory,
you should not see any compile-time errors or warnings.
java
command:
~/hello> java HelloWorld Hello, WorldYou should see the output of your program beneath the line on which you typed the command.
Tip
Typically, you should compile from IntelliJ (because IntelliJ highlights the lines on which any compile-time errors or warnings occur) and execute from the command line (because the command line makes it is easy to specify command-line arguments and use file redirection).
8. Textbook Libraries (from the command line) |
To make our textbook libraries accessible to Java from the command line,
you will use our wrapper scripts.
stdlib.jar
to draw a
Barnsley fern.
hello
).
~/hello> ls Barnsley.java COS 126.iml HelloWorld.java logo.png ~/hello> javac-introcs Barnsley.java ~/hello> java-introcs Barnsley 10000
To get your command prompt back, close the standard drawing window.
algs4.jar
to simulate the motion of n disks subject to the laws
of elastic collision.
percolation
).
~/hello> ls CollidingDisks.java COS 226.iml WELCOME.txt logo.png ~/hello> javac-algs4 CollidingDisks.java ~/hello> java-algs4 CollidingDisks 20
When you execute the program, a standard drawing window will appear with an animation of 20 colliding disks.
To get your command prompt back, close the standard drawing window.
Frequently Asked Questions |
apt
,
zypper
,
emerge
, or
yum
) to install Java.
~/hello> javac -version javac 11.0.24 ~/hello> java -version openjdk version "11.0.24" 2022-04-19 OpenJDK Runtime Environment Temurin-11.0.24+8 (build 11.0.24+8) OpenJDK 64-Bit Server VM Temurin-11.0.24+8 (build 11.0.24+8, mixed mode) ~/hello> type javac /usr/bin/javac ~/hello> type java /usr/bin/javaIt’s important that the Java version numbers match and that you see the number
11
,
but the rest is not critical.
The course-specific project folders perform additional customizations:
java.lang
,
java.util
, and algs4.jar
).
algs4.jar
).
!
as the last character
in the project folder (or any directory name along the path to your project folder);
that will confuse both IntelliJ and Checkstyle.
.iml
file (which defines the project),
the .idea
subdirectory (which contains
the IntelliJ course preferences), and
the .lift
subdirectory (which contains the course
libraries).
To create a new project from scratch, you can use the Create New Project option from the Welcome screen. But, we do not recommend this approach for novice programmers.
javac-algs4
or java-algs4
.
javac-algs4
and
java-algs4
)
should already be available.
Our autoinstaller customizes the command line in a few ways by copying these three configuration files:
.bashrc
,
.bash_profile
, and
.inputrc
.
Ctrl-C
.
Enter
Ctrl-D
.
On Windows, type Enter
Ctrl-Z
Enter
,
even in Git Bash.
~/hello> spotbugs HelloWorld.class Running spotbugs on HelloWorld.class:The argument must be a list of
.class
files.
Here is a list of
bug descriptions.
~/hello> pmd HelloWorld.java Running pmd on HelloWorld.java:The argument must be either a single
.java
file or
a directory containing one or more .java
files.
Here is a list of
bug patterns.
~/hello> checkstyle -cos126 HelloWorld.java Running checkstyle on HelloWorld.java: ~/hello> checkstyle -cos226 HelloWorld.java Running checkstyle on HelloWorld.java: ~/hello> checkstyle -coursera HelloWorld.java Running checkstyle on HelloWorld.java:The argument must be a list of
.java
files.
Here is a list of
available checks.
/usr/local/bin
is in my PATH
environment variable?
~> echo $PATH /usr/local/bin:/usr/bin:/usr/local/sbin:usr/sbinYou should see the entry
/usr/local/bin
,
where entries are delimited by the colon :
character.