Hello World in Java (macOS)
This guide explains how to configure a Java programming environment
on macOS
,
create and compile a Java program in IntelliJ,
and run it from the command line.
You will need an Apple silicon Mac running
macOS 15 (Sequoia) to
macOS 26 (Tahoe).
|
0. Install the Java Programming Environment
|
The installer configures
OpenJDK 21,
IntelliJ IDEA, OSS Edition 2026.2,
and the course tools and libraries.
-
Sign in to the macOS account you will use for programming.
The account must have administrator privileges.
Download lift-java-arm64.pkg.
(If you have an older Intel Mac, use lift-java-x64.pkg.)
-
Double-click the downloaded package.
Enter your macOS password when prompted and accept the default options.
Warning
If you have previously customized IntelliJ,
run the installer only if you want a clean reinstall.
It replaces IntelliJ IDEA OSS.app and
its associated user settings.
By default, it also replaces existing Bash configuration files,
including .bashrc.
To preserve these files, see the FAQ.
-
After installation, you may delete lift-java-arm64.pkg.
|
1. 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.
-
Download the project for your programming assignment to a convenient location
(such as the Desktop).
[ 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) ]
If needed, double-click the ZIP file to extract it.
This 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.
-
Launch IntelliJ via Finder → Applications → IntelliJ IDEA OSS.app.
-
When you launch IntelliJ for the first time,
-
It may display the
JetBrains privacy policy.
Scroll down and Accept.
- It may ask if you want to send anonymous usage
statistics to JetBrains. Choose your preferred option.
- It will display the Welcome to IntelliJ IDEA
screen.
-
To open a project from the Welcome to IntelliJ IDEA screen,
click Open and select the project folder.
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 (⌘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.
-
When you are finished working, select
IntelliJ IDEA → Quit IntelliJ IDEA (⌘Q) to exit IntelliJ.
The next time you launch IntelliJ, your recent projects
will appear in the Welcome to IntelliJ IDEA screen for easy access.
|
2. 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.
-
To create a new Java program:
- Reopen IntelliJ and the project (if you closed it in the previous step).
-
Select the project name in the left Project View sidebar.
If you don't see the Project View sidebar,
select LIFT → Project (⌘1) to toggle it on.
-
Select LIFT → New Java Class.
Enter HelloWorld as the Name and press Return.
-
In the main editor window, complete
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");
}
}
|
-
As you type, IntelliJ highlights different syntax,
inserts matching brackets, and indents new lines.
-
Select File → Save All (⌘S) to save and, if necessary,
reformat your code.
IntelliJ also saves automatically before events such as
as compiling, running, closing a project, or quitting.
|
3. Compile and Run the Program (from IntelliJ)
|
Before Java can execute (or run) your program,
it must compile the source code.
-
Select the program that you wish to compile and run in the Project View sidebar.
The program should now appear in the main editor window.
-
To compile your program,
select LIFT → Recompile 'HelloWorld.java' (⌘B).
If compilation succeeds, a confirmation appears in the status bar.
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.
-
To run your program,
select LIFT → Run 'HelloWorld' with Arguments (⌘E).
Because the program takes no command-line arguments, click OK without entering any.
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.
|
4. 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.
-
Select LIFT → Terminal (⌘2).
-
This opens an embedded
Bash terminal.
You will see a command prompt that looks something like this:
~/Desktop/hello>
~/Desktop/hello is the current working directory;
~ denotes your home directory.
-
To compile your program,
type the following
javac command
(highlighed in yellow below).
~/Desktop/hello> javac HelloWorld.java
~/Desktop/hello>
If HelloWorld.java is in the current directory
and contains no errors,
the javac command produces no output.
-
To run your program,
type the following
java command:
~/Desktop/hello> java HelloWorld
Hello, World
You 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.
|
5. Textbook Libraries (from the command line)
|
To make our textbook libraries accessible to Java from the command line,
you will use our wrapper scripts.
-
Computer Science: An Interdisciplinary Approach (including COS 126 students).
The program
Barnsley.java
uses our standard drawing and standard random libraries
in
stdlib.jar to draw a
Barnsley fern.
When you run the program, the standard drawing window opens
and generates the image one point at a time.
To get your command prompt back, close the standard drawing window.
-
Algorithms, 4th Edition (including COS 226 students).
The program
CollidingDisks.java
uses classes from
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:
~/Desktop/hello> ls
CollidingDisks.java COS 226.iml WELCOME.txt logo.png
~/Desktop/hello> javac-algs4 CollidingDisks.java
~/Desktop/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
|
Expand All
Collapse All
-
I installed IntelliJ and Java using the
lift-java-arm64.pkg installer
last semester or year. Should I run it again this semester?
-
Yes. This installer installs OpenJDK 21
and IntelliJ 2026.2.
The old installer may have used an earlier version of Java or
IntelliJ.
-
What happens if I rerun the lift-java-arm64.pkg installer?
-
It will reinstall and reconfigure
OpenJDK 21, IntelliJ,
SpotBugs, PMD, Checkstyle, our textbook libraries,
and wrapper scripts.
-
Can I install only selected components?
-
Run the installer. On the Installation Type screen, click Customize
and select the desired components:
- OpenJDK 21: installs Java.
- Command-Line Tools: installs wrapper scripts like
javac-introcs and
javac-algs4
- Bash Configuration Files: installs our
.bashrc,
.bash_profile, and
.inputrc files.
- IntelliJ IDEA, OSS Edition 2026.2: installs IntelliJ
with our novice-friendly user preferences.
We generally recommend a full installation. A custom installation can be useful if you
already have a specialized
.bashrc file.
It might also speed things up
if you need to reinstall, say IntelliJ IDEA, OSS Edition 2026.2, but not Java.
-
The installer overwrote my specialized
.bashrc file. How can I revert to the original version?
-
If you didn't want this to happen, it's best to
unmark the Bash Configuration Files component during installation, as described in the
previous question.
If it's too late for that, the autoinstaller
creates a timestamped backup, such as
.bashrc.20210706-191748.
Rename or copy the most recent backup to .bashrc.
-
The installer failed. How can I investigate why?
-
Check the installer log at
/var/log/lift.log.
-
I have macOS 14.0 (Sonoma). Is that too old?
-
Yes, sorry. IntelliJ 2026.2 requires macOS 15.0 (Sequoia) or newer.
-
How long will the installer take to complete installation?
-
After downloading, installation typically takes about one minute.
With anti-malware software (such as McAfee Endpoint), it may take 5–10 minutes.
-
What does the lift-java-arm64.pkg installer do?
-
In short, it installs and configures
Java, IntelliJ,
SpotBugs,
PMD,
Checkstyle,
and our textbook libraries,
along with accompanying command-line tools.
Here is a more detailed list:
-
How is the software licensed?
-
All of the included software is licensed under various open-source licenses.
-
What’s the SHA-256 checksum of lift-java-arm64.pkg?
-
6feadf08beba0de0135840e8c768f9157883f6c2635618931d48d62beca81984.
-
How can I run the installer from the command line?
-
From the target directory containing the package,
type the command
sudo installer -verbose -pkg lift-java-arm64.pkg -target /
-
How can I uninstall the software?
-
- To uninstall OpenJDK 21,
delete the following directory:
-
/Library/Java/JavaVirtualMachines/temurin-21.jdk/
- To uninstall IntelliJ, delete the following:
-
/Applications/IntelliJ IDEA OSS.app
-
~/Library/Application Support/JetBrains/IdeaIC2026.2/
-
~/Library/Caches/JetBrains/IdeaIC2026.2/
-
~/Library/Logs/JetBrains/IdeaIC2026.2/
- To uninstall SpotBugs, PMD, Checkstyle,
and our textbook libraries, delete the following directory:
- To uninstall our wrapper scripts, delete the following files:
-
/usr/local/bin/javac-{algs4,introcs}
-
/usr/local/bin/java-{algs4,introcs}
-
/usr/local/bin/jshell-{algs4,introcs}
-
/usr/local/bin/spotbugs
-
/usr/local/bin/pmd
-
/usr/local/bin/checkstyle
-
Can I use a different Java or version?
-
Yes.
You may use any version of Java 11 or newer from either Oracle or OpenJDK.
If you do, you may need to configure the Platform SDK and Project SDK manually in
IntelliJ under File → Project Structure.
We recommend using long-term support (LTS) versions: Java 11, Java 17, Java 21, and Java 25.
-
How can I check which version of Java is installed (and where it is installed)?
-
Type the following commands in the terminal:
~/Desktop/hello> javac -version
javac 21.0.11
~/Desktop/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)
~/Desktop/hello> /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home
With the default installation,
javac and java
should both report version 21.
-
How does this customized version of IntelliJ different from the standard one?
-
IntelliJ
is an industrial-strength integrated development environment (IDE),
suitable for use by professional programmers.
The installer configures your user preferences to make it
more suitable for use by novice programmers:
- Disables all built-in plugins except Terminal and JUnit.
Installs the SpotBugs, Checkstyle-IDEA,
Run-with-Arguments,
Save-Actions,
and Archive browser plugins.
- Eliminates or reduces various pop-up elements
(lightbulbs, code folding, breadcrumbs, gutter markers, notifications, parameter hints).
- Simplifies menus and toolbars, hiding advanced options.
- Disables live templates and postfix completion.
- Adopts the Obsidian Black color scheme.
- Configures Java automatically.
- Adds a few keyboard shortcuts.
The course-specific project folders provide additional customizations:
- Streamlines autocomplete to display only relevant libraries
(such as
java.lang,
java.util, and algs4.jar).
- Configures SpotBugs and Checkstyle with course-specific rules.
- Provides course-specific libraries (such as
algs4.jar).
- Enables automatic formatting of code on save.
- Enables automatic importing of Java libraries.
-
How can I manually configure the Platform SDK and Project SDK in IntelliJ?
-
The installer should configure the Platform SDK automatically.
To configure it manually,
- Navigate to File → Project Structure → Platform Settings → SDKs.
- Click the + symbol (upper left) to add an SDK.
- Select the JDK installation directory. A typical location for a JDK on
macOS is
/Library/Java/JavaVirtualMachines/temurin-21.jdk/Contents/Home.
- Use the shorthand name suggested by IntelliJ
(e.g., 21 for version 21.0.11).
To manually configure the Project SDK,
- Navigate to File → Project Structure → Project Settings → Project.
- Choose the desired Project SDK from the drop-down list.
- Be sure to use 21 as the Project language level, as our autograder
currently supports only Java 21 features.
-
Where should I store my projects?
-
Store projects on a local drive (not a network drive, cloud-synced folder, or SMB share).
Create one course folder with a separate subfolder for each assignment.
-
My IntelliJ project does not work properly if I save it under the Desktop,
Documents, or Downloads. But it works fine if I save it elsewhere. Why?
-
macOS may restrict access to the
Desktop, Documents, and Downloads folders until you grant permission.
If you previously denied access, go to System Settings → Privacy & Security → Files & Folders→ IntelliJ.
-
Are there any special characters to avoid when naming IntelliJ projects or files?
-
Do not end the name of a project folder (or any enclosing directory) with an
exclamation mark (
!);
doing so confuse IntelliJ and Checkstyle.
-
How can I create a new project in IntelliJ?
-
If you want to inherit all of the properties of an existing project,
- Use the Finder to copy the project folder,
giving it your preferred name.
- Delete any unwanted files.
- Be sure to keep
the
.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.
-
What are the most important IntelliJ menu options to remember?
-
Here are the most important ones (and their shortcuts).
- LIFT → New Java Class (⌘N). Create a new Java class.
- LIFT → Recompile (⌘B). Compile the current program.
- LIFT → Run with Arguments (⌘E). Run the current program with command-line arguments.
- File → Save All (⌘S). Save (and reformat) all open files.
- View → Tool Windows → Project (⌘1). Show/hide the Project View sidebar.
- View → Tool Windows → Terminal (⌘2). Show/hide the Terminal window.
-
Can I use a version of IntelliJ that is newer than 2026.2.1?
-
Yes, though if it is 2026.3 (or above),
you will need to migrate your user preferences.
-
How can I restore the original IntelliJ settings
(instead of the custom novice-friendly ones)?
-
- To restore the menus and toolbars: Settings → Appearances & Behavior → Menus and Toolbars → Restore All Defaults.
- To restore all settings: File → Manage IDE Settings → Restore Default Settings.
-
When I compile or run a program from the command line that uses one of the
textbook libraries, I get an error that it cannot find the library. How can I fix this?
-
Make sure that you are using the appropriate wrapper script,
such as
javac-algs4
or java-algs4.
-
How should I configure Bash?
-
If you followed our instructions, our wrapper scripts (such as
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.
-
How do I break out of a program in an infinite loop?
-
Type
Ctrl-C.
-
How do I specify EOF to signal that standard input is empty?
-
On macOS and Linux, press
Enter, then Ctrl-D.
On Windows, press Enter, Ctrl-Z,
and then Enter, even in Git Bash.
-
How can I run SpotBugs, PMD, and Checkstyle from the command line?
-
The installer includes wrapper scripts to simplify this process.
-
To run SpotBugs 4.8.4, type the following command in the terminal:
~/Desktop/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.
-
To run PMD 6.34.0, type the following command in the terminal:
~/Desktop/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.
-
To run Checkstyle 10.26.1, type one of
the following commands in the terminal, depending on whether you are COS 126, COS 226,
or Coursera student:
~/Desktop/hello> checkstyle -cos126 HelloWorld.java
Running checkstyle on HelloWorld.java:
~/Desktop/hello> checkstyle -cos226 HelloWorld.java
Running checkstyle on HelloWorld.java:
~/Desktop/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.
-
How can I change the default shell in the Terminal application from
zsh
to bash?
-
macOS uses
zsh as the default shell.
To use bash instead,
run the following command in Terminal and
enter your macOS password:
~/Desktop/hello> chsh -s /bin/bash
Finally, close the Terminal application and reopen it.
Note that the embedded terminal within IntelliJ is
configured to use bash regardless of
the default shell.
-
When I try to compile with
javac, I get an "error while writing"
message. How can I fix this?
-
Anti-malware software such as Bitdefender may prevent Bash from writing class files.
If so, designate
bash as a safe program.