This guide explains how to configure a Java programming environment
on Linux
,
create and compile a Java program in IntelliJ,
and run 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 21 (JDK 21).
Note
Skip this step if you already have JDK 21 installed.
~>
The symbol ~ is shorthand for your home directory.
~> sudo apt-get update ~> sudo apt-get install openjdk-21-jdk
~> javac -version javac 21.0.11 ~> java -version openjdk version "21.0.11" 2026-04-21 LTS OpenJDK Runtime Environment (build 21.0.11+10-LTS) OpenJDK 64-Bit Server VM (build 21.0.11+10-LTS, mixed mode, sharing)It’s important that the Java version numbers match and that you see the number
21,
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 apt-get install curl /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.
aarch or x64 version
depending on your machine architecture.
After installing it, create a desktop shortcut by following
these instructions
for a standalone Linux installation.
Warning
Be sure to choose the Community Edition and 2026.2. Our customized IntelliJ preferences won't be active if you choose the Ultimate Edition or 2026.3.
~> cd ~> rm -rf .cache/JetBrains/IdeaIC2026.2 ~> rm -rf .config/JetBrains/IdeaIC2026.2 ~> rm -rf .local/share/JetBrains/IdeaIC2026.2 ~> curl -O "https://lift.cs.princeton.edu/java/linux/IdeaIC2026.2.zip" ~> unzip IdeaIC2026.2.zip ~> rm IdeaIC2026.2.zip
Warning
This will replace any previous IntelliJ 2026.2 settings with our novice-friendly settings.
| 4. Open a Project in IntelliJ |
You will develop your Java programs in IntelliJ,
which organizes programs into projects.
In our context, each project corresponds to one programming assignment
and contains source files, data files, and course-specific settings.
[ 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) ]
Extract 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).
You may delete the ZIP file.
Warning
Each project contains course-specific information. Be sure to download the project for 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
Open the project folder, not an individual file Do not select New Project, which is intended for advanced programmers./usr/lib/jvm/java-21-openjdk-amd64/.
| 5. Create a Program in IntelliJ |
Now you are ready to write your first Java program.
IntelliJ provides syntax highlighting, bracket matching,
automatic indentation and formatting, automatic imports, variable renaming,
and continuous code inspection.
HelloWorld.java so that
it matches the code below.
Note that IntelliJ generates the gray boilerplate code automatically.
A missing semicolon will prevent the program from compiling.
public class HelloWorld { // prints "Hello, World" public static void main(String[] args) { System.out.println("Hello, World"); } }
| 6. Compile and Run the Program (from IntelliJ) |
Before Java can execute (or run) your program,
it must compile the source code.
If it fails, the Recompile panel opens at the bottom and lists any errors or warnings. Check your program carefully for typos, using the error messages as a guide.
You should see the output of the program (highligted in white below), along with a message that the program finished normally (with exit code 0).
Tip
Use the LIFT menu to compile and run your program from IntelliJ. The Build and Run menus support additional options for advanced programmers.
Before using the LIFT menu, click inside the editor window to make it active.
| 7. Compile and Run the Program (from the command line) |
The command line provides a simple and powerful way to control programs
using command-line arguments, file redirection, and piping.
IntelliJ includes an embedded terminal
for convenient access.
~/hello>
~/hello is the current working directory;
~ denotes your home directory.
javac command
(highlighed in yellow below).
~/hello> javac HelloWorld.java ~/hello>If
HelloWorld.java is in the current directory
and contains no errors,
the javac command produces no output.
java command:
~/hello> java HelloWorld Hello, WorldYou should see the output of your program beneath the line on which you typed the command.
Tip
For routine work, compile compile in IntelliJ, which identifies errors by line, and run from the command line, which makes command-line arguments and file redirection easier.
| 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).
Then compile and run it with the following commands:
~/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.
Download
CollidingDisks.java
and move it to a project folder (such as hello).
Then compile and run it by typing the following commands:
~/hello> ls CollidingDisks.java COS 226.iml WELCOME.txt logo.png ~/hello> javac-algs4 CollidingDisks.java ~/hello> java-algs4 CollidingDisks 20
When you run the the program, the standard drawing window opens and animates 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 21.0.11 ~/hello> java -version openjdk version "21.0.11" 2026-04-21 LTS OpenJDK Runtime Environment Temurin-21.0.11+10-LTS (build 21.0.11+10-LTS) OpenJDK 64-Bit Server VM Temurin-21.0.11+10-LTS (build 21.0.11+10-LTS, mixed mode, sharing) ~/hello> type javac /usr/bin/javac ~/hello> type java /usr/bin/javaWith the default installation,
javac and java
should both report version 21.
The course-specific project folders provide additional customizations:
java.lang,
java.util, and algs4.jar).
algs4.jar).
!);
doing so confuse 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 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.
The installer configures Bash by copying
.bashrc,
.bash_profile, and
.inputrc.
If you accepted the default installation options, no additional configuration is required.
Ctrl-C.
Enter, then Ctrl-D.
On Windows, press Enter, Ctrl-Z,
and then 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.