This document instructs you on how to set up a Java programming environment
for your macOS
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 Mac running macOS 12.0 (Monterey) to macOS 14.6 (Sonoma). Both Apple Silicon Macs (e.g., M1, M2, M3) and Intel Macs are supported.
0. Install the Java Programming Environment |
The installer installs and configures a Java programming environment, including
OpenJDK 11 and
IntelliJ IDEA, Community Edition 2024.2.
Warning
If you have previously used IntelliJ, run this installer only if you want a clean re-install. The installer overwrites IntelliJ IDEA CE.app and the accompanying user settings.
Also, by default, the installer overwrite any pre-existing Bash configuration files,
such as .bashrc
.
If you wish to preserve your version, see the FAQ.
1. 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 (⌘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.
2. 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 (⌘S) frequently for its code reformatting functionality.
3. 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).
4. 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.
~/Desktop/hello>
The ~/Desktop/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.
~/Desktop/hello> javac HelloWorld.java ~/Desktop/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:
~/Desktop/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).
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.
stdlib.jar
to draw a
Barnsley fern.
hello
).
~/Desktop/hello> ls Barnsley.java COS 126.iml HelloWorld.java logo.png ~/Desktop/hello> javac-introcs Barnsley.java ~/Desktop/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
).
~/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 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 |
javac-introcs
and
javac-algs4
.bashrc
,
.bash_profile
, and
.inputrc
files.
Typically, we recommend a full install. A custom install can be useful if you
already have a specialized
.bashrc
file.
It might also speed things up
if you need to re-install, say IntelliJ IDEA, Community Edition 2024.2, but not Java.
.bashrc
file. How can I revert to the original version?
.bashrc
file by appending
a timestamp to the filename, e.g., .bashrc.20210706-191748
.
/var/log/lift.log
.
stdlib.jar
and
algs4.jar
.
javac-algs4
and
java-algs4
.
.bashrc
,
.bash_profile
, and
.inputrc
files.
spotbugs.xml
;
and wrapper script spotbugs
.
pmd.xml
;
and wrapper script pmd
.
checkstyle-cos126.xml
,
checkstyle-cos226.xml
,
checkstyle-coursera.xml
, and
checkstyle-suppressions.xml
);
custom checks checkstyle-lift.jar
;
and wrapper script checkstyle
.
7e348478f8124916e031257a167c117f34c500ce294228a39cd4ad1f2bec5a4c
and
c3861ff00ca36cc129e4b6b86fc5d96ed4b4d49a5e4fc9fcd7a971fde2e46340
.
sudo installer -verbose -pkg -target /
/Library/Java/JavaVirtualMachines/temurin-11.jdk/
/Applications/IntelliJ IDEA CE.app
~/Library/Application Support/JetBrains/IdeaIC2024.2/
~/Library/Caches/JetBrains/IdeaIC2024.2/
~/Library/Logs/JetBrains/IdeaIC2024.2/
/usr/local/lift/
/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
~/Desktop/hello> javac -version javac 11.0.24 ~/Desktop/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)It’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.
~/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.
~/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.
~/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.
zsh
to bash
?
zsh
.
To change it to bash
, type the following command
into the Terminal application and enter your macOS password:
~/Desktop/hello> chsh -s /bin/bashFinally, close the Terminal application and reopen it. Note that the embedded terminal within IntelliJ is configured to use
bash
regardless of
the default shell.
javac
, I get an "error while writing"
message. How can I fix this?
bash
as a Safe Program.