This document instructs you on how to set up a Java programming environment
for your Windows
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.
Warning
If you have previously used IntelliJ 2023.2, run this installer only if you want a clean re-install. The installer overwrites any existing IntelliJ 2023.2 settings with our novice-friendly settings.
The installer also performs a clean re-install of Git Bash,
which will overwrite any previous user settings, including .bashrc
.
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) ]
To unzip the zip file, right click it and select Extract All.
This 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.
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 (Ctrl + 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.
~>
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.
~> javac HelloWorld.java ~>Assuming that the file
HelloWorld.java
is in the current working directory,
you should not see any compile-time errors or warnings.
java
command:
~> 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
).
~> ls Barnsley.java COS 126.iml HelloWorld.java logo.png ~> javac-introcs Barnsley.java ~> 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
).
~> ls CollidingDisks.java COS 226.iml WELCOME.txt logo.png ~> javac-algs4 CollidingDisks.java ~> 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 |
C:\Program Files\LIFT-CS
and launch the uninstaller unins000.exe
.
C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2023.2\bin
;
right click the uninstaller Uninstall.exe
;
and select Run as administrator.
%TEMP%\LIFT-CS\
and
%TEMP%\Setup Log YYYY-MM-DD #NNN.txt
,
where
%TEMP%
is typically
C:\Users\<Username>\AppData\Local\Temp
,
YYYY-MM-DD
is the current date,
and NNN
is an integer.
PMD is licensed under a BSD-style license.
a4b2e98e71ad7cff9118bf6612e1610fb7caaebd315da7e00d8457115b5ec18f
~> javac -version javac 11.0.20 ~> java -version openjdk version "11.0.20" 2022-04-19 OpenJDK Runtime Environment Temurin-11.0.20+8 (build 11.0.20+8) OpenJDK 64-Bit Server VM Temurin-11.0.20+8 (build 11.0.20+8, mixed mode) ~> which javac c/Program Files/Eclipse Adoptium/jdk-11.0.20.10-hotspot/bin/javac ~> which java c/Program Files/Eclipse Adoptium/jdk-11.0.20.10-hotspot/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
).
C:\Program Files\Java\jdk11.0.20
.
To manually configure the Project SDK,
!
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
.
bash
with
"C:\Program Files\Git\bin\bash"
.
javac-algs4
or java-introcs
).
How can I fix this?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.
~> spotbugs HelloWorld.class Running spotbugs on HelloWorld.class:The argument must be a list of
.class
files.
Here is a list of
bug descriptions.
~> 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.
~> checkstyle -cos126 HelloWorld.java Running checkstyle on HelloWorld.java: ~> checkstyle -cos226 HelloWorld.java Running checkstyle on HelloWorld.java: ~> 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.
javac-algs4
and
checkstyle-algs4
will work only in Git Bash. If you want to use another shell, you’ll need
to configure it yourself.
C:\Program Files\Git\usr\bin
.