2. Setting Up the Development Environment
The Java developer’s work environment consists of three independent components:
- the JDK (“Java Development Kit”): the [javac] compiler, the virtual machine and the [java] command that runs programs, the standard Java libraries SE, and tools such as [jshell] or [jar];
- a project build tool, in this case Maven: it downloads the libraries a project needs, compiles it, runs its tests, and packages it;
- an editor or an IDE (Integrated Development Environment): [IntelliJ IDEA], [VS Code] with Java extensions, or [Eclipse]. IDE is based on JDK and can read Maven projects.
This document uses the commands [java], [javac], and [mvn] for all operations: they work the same way on Windows, Linux, and macOS, regardless of the editor you choose.
2.1. JDK 25
2.1.1. Installation
There are several distributions of JDK, all built from the OpenJDK source code and equivalent for our purposes. We recommend Eclipse Temurin, a free and open-source distribution from the Eclipse Foundation, which can be downloaded at https://adoptium.net: There, select version 25—LTS—the operating system, and the package type (on Windows, a [.msi] installer). On Windows, you can also install it from a terminal (PowerShell or command prompt) using the [winget] package manager:
You must select JDK (which contains everything) and not just JRE (which only allows you to run programs that have already been compiled). When installing on Windows, check the options that add JDK to [PATH] and set the [JAVA_HOME] environment variable: Maven and the IDE packages use this to locate JDK. On Linux, JDK is available in the repositories of major distributions (for example, [sudo apt install openjdk-25-jdk] on Ubuntu).
After installation, open a new terminal to verify that the [java] and [javac] commands are available and display the version of JDK:
These results were obtained with the JDK package from Ubuntu. With Temurin, lines 2 and 3 list [Temurin-25], and the exact version depends on the installation date: patch updates are released quarterly. All JDK and 25.0.x versions are suitable for this document. Multiple JDK files can coexist on the same system: the first one found in the [PATH] directory responds to the [java] command.
2.1.2. Commands for the JDK
The JDK provides numerous commands. Here are the ones we will use:
Command | Role |
[java File.java] | compiles in memory and executes a program written in a source file (Java 11; multiple files starting with Java 22) |
[javac -d classes File.java] | compiles one or more source files and places the resulting [.class] files in the [classes] folder |
[java -cp classes ClassName] | executes the [main] method of a compiled class, searched for in the class path ([-cp]) |
[java -cp "lib/*" File.java] | executes a source file that uses the libraries ([.jar] files) in the [lib] folder |
[jshell] | Interactive interpreter: you type Java statements and see their results immediately |
[jar] | creates or reads a [.jar] archive (a ZIP file of compiled classes) |
The [jshell] interpreter (Java 9) is useful for testing a method or expression without writing a program:
2.2. Maven
2.2.1. Installation
A real Java application uses libraries that are not part of JDK: a database driver, a testing framework, Hibernate, Spring… These are distributed as [.jar] archives via a public repository, Maven Central (https://central.sonatype.com). A build tool like Maven reads the [pom.xml] file of a project to determine which libraries it needs, downloads them (along with the libraries on which they depend), and then compiles, tests, and packages the project. Another widely used tool, Gradle, performs the same function; this document uses Maven.
Maven can be downloaded from https://maven.apache.org/download.cgi as a ZIP archive ([apache-maven-3.9.x-bin.zip]). Unzip it into a folder, for example [C:\Program Files\apache-maven-3.9.11], and add its subfolder [bin] to [PATH] (Windows: “Edit system environment variables”). On Linux, Maven is available in the distribution repositories ([sudo apt install maven]). Verify the installation in a new terminal:
Line 4 is important: it shows the JDK used by Maven, which is referenced by the variable [JAVA_HOME]. It must be a JDK 25. The IDE, IntelliJ, IDEA, and VS Code packages include their own Maven: these are sufficient if you do not use Maven from the command line.
The libraries downloaded by Maven are stored on the local machine in the local repository [%USERPROFILE%\.m2\repository] (Linux, macOS: [~/.m2/repository]). They are downloaded only once and are then used by all projects. Therefore, an Internet connection is required when building a project for the first time.
2.2.2. Maven Commands
The command [mvn] is followed by one or more phases of the build cycle, or by a plugin target; it is entered in the folder containing the file [pom.xml]:
Command | Role |
[mvn compile] | compiles the source code of [src/main/java]; the classes are organized in [target/classes] |
[mvn test] | compiles and then runs the unit tests for [src/test/java] (Chapter 6) |
[mvn package] | compiles, tests, and creates the project archive [.jar] in [target] |
[mvn install] | as [package], then copies the archive to the local repository, where other projects can use it (Chapter 6) |
[mvn clean] | deletes the folder [target] |
[mvn compile exec:java] | compiles and then runs the project’s main class ([exec-maven-plugin] plugin) |
[mvn spring-boot:run] | runs a Spring Boot application (Chapter 13) |
The phases are sequential: running [package] also triggers [compile] and [test]. The [-q] (“quiet”) option suppresses Maven messages and displays only those from the executed program; the [-Dexec.args="a b"] option passes arguments to the program launched by [exec:java].
2.3. The editor
2.3.1. IntelliJ IDEA
IntelliJ IDEA, from the company JetBrains, is the most widely used Java editor. Its basic features, which are more than sufficient for this document, are free. It supports Maven projects: select “File / Open,” then choose the folder containing the [pom.xml] file; IntelliJ downloads the libraries, compiles the project, and allows you to run a class by clicking the green triangle in the margin next to its method [main] ([Ctrl-Shift-F10]), or run a test by clicking the same triangle next to the test method. Specify the JDK to use in “File / Project Structure / SDK.”
You can open the entire [java-oct-2026] examples folder at once (“File / Open”): IntelliJ detects all the Maven projects there and displays them in its “Maven” panel. One precaution is necessary at this point. The projects in Chapters 10 through 13 (versions 6 through 9 of the [Tax] application) use the modules [tax-dao], [tax-business], and [tax-ui] from version 5, which was written in Chapter 6. These are not public libraries: Maven can only find them in the local repository if they have been installed there. You must therefore start by installing version 5: in the “Maven” panel, expand [tax-v5] / "Lifecycle" and double-click on [install] (or type [mvn install] in the [06-architectures/TaxV5] folder), then click the "Sync All Maven Projects" button in the panel. Chapter 6 explains what this command does and what to do if IntelliJ still reports unresolved dependencies.
2.3.2. VS Code
VS Code is a free, cross-platform editor. To program in Java, you can install Microsoft’s Extension Pack for Java (go to the “Extensions” menu and search for “Extension Pack for Java”), which includes language support, a debugger, test execution, and Maven support. This provides:
- syntax highlighting, code completion, and tooltip help;
- error detection as you type;
- code navigation (F12: go to definition);
- execution and debugging (the “Run” and “Debug” links above each method [main] or F5);
- running unit tests (the “Testing” view) and a “Maven” view of projects.
In VS Code, open the folder containing the programs (“File / Open Folder”), for example, the [java-oct-2026] folder with the examples from this document. The built-in terminal (“Terminal / New Terminal”) allows you to enter the commands [java] and [mvn] without leaving the editor.
2.3.3. Eclipse
A third option is the open-source Eclipse IDE (https://eclipseide.org), historically the first major Java IDE, which also imports Maven projects (“File / Import / Maven / Existing Maven Projects”).
In the rest of this document, operations are described using the commands [java], [javac], and [mvn], which produce the same results regardless of the editor.
2.4. A First Program: A Compact Source File
2.4.1. The program
Starting with Java 25, a Java program can be a simple file containing a single method [main], without a class declaration. This is called a “compact source file.” Let’s create the following file [Hello.java]:
and run it:
Comments:
- line 1: a comment. Everything following [//] until the end of the line is ignored by the compiler;
- line 2: the method [main], the program’s entry point. In a compact source file, it does not need to be declared in a class, nor does it need to be [public] and [static], nor does it need to receive the program’s arguments; we’ll see in Chapter 3 that the compiler wraps it in a class itself;
- Line 3: the program’s only statement: it prints a line of text to the screen. The class [IO] (Java 25) provides simple methods for writing ([print], [println]) and reading ([readln]) from the keyboard;
- the command [java Hello.java] compiles the program in memory—without creating a [.class] file—and then executes it. This ability to run a source file directly was introduced in Java 11.
The file name must be a valid Java identifier: [Hello.java] or [Basics01.java] are acceptable, but [basics-01.java] is rejected by the compiler ([bad file name]) because the file name becomes the name of the generated class. By convention, class names—and therefore file names—begin with a capital letter.
You can also compile the program with [javac], which produces a file named [.class], and then run this compiled file:
The file [Hello.class] contains the bytecode for the [Hello] class generated by the compiler. The command [java -cp classes Hello] instructs the virtual machine to execute the method [main] of the class [Hello], which is located in the [classes] folder.
Compact source files are suitable for small programs, and we will use them for most of the examples. When a program requires an external library, it can still be launched using the [java -cp "lib/*" Program.java] command if you have copied the [.jar] archives of the library (and those it depends on) into a [lib] folder. But gathering these archives by hand quickly becomes tedious, so we’ll write a Maven project.
On Windows, accented characters written by [IO.println] or [System.out.println] display correctly in the terminal: Java converts the text to the terminal’s encoding. The source files must be saved in UTF-8, the default encoding for VS Code and IntelliJ IDEA, and the one the compiler assumes starting with Java 18.
2.5. A Maven Project
2.5.1. Project Structure
A Maven project is a folder containing a [pom.xml] file (“Project Object Model”) and source files organized according to a standard directory structure. The [FirstProgram] project in the [02-installation] folder is as follows:
- [src/main/java] contains the program's source files; [src/main/resources] contains the data or configuration files to be placed alongside the compiled classes; [src/test/java] contains the tests (Chapter 6);
- Under [src/main/java], the folder structure mirrors that of the packages (Chapter 4): the class [first.Main] is located in the folder [first].
You can create this directory structure manually, or let IDE create it (IntelliJ: “File / New / Project,” select “Maven” as the build system). The [pom.xml] file is as follows:
- Lines 8–10: the project’s coordinates, which uniquely identify it: the group ([groupId], often a reverse domain name such as [org.hibernate]), the project name ([artifactId]), and its version. These are also the identifiers that another project will use to reference this one if needed, similar to how a library from Maven Central is referenced (Chapter 6);
- line 14: the code is compiled for Java 25;
- line 15: the source files are written in UTF-8;
- lines 21–30: the compilation plugin, with the option [-Xlint:all], which enables all compiler warnings;
- lines 32–39: the [exec-maven-plugin] plugin, which allows the project’s main class—specified on line 37—to be executed using the [mvn exec:java] command.
This project does not use any libraries. A project that uses a library declares it in a [<dependencies>] section using its coordinates, as we will do starting in Chapter 5:
The file [Main.java] contains the program in its standard form, with a class [public] and a method [public static void main(String[] args)]:
- line 1: the class belongs to the package [first]; its full name is [first.Main];
- lines 4–9: the class [Main]. A source file contains at most one public class, which bears the name of the file;
- lines 6–8: the method [main], in the form it has had since the first version of Java. We will compare the two forms in Chapter 3.
2.5.2. Compilation and Execution
The command [mvn compile], entered in the project folder, compiles the project: Maven downloads any required plugins as needed, then compiles the [src/main/java] files and places the resulting classes in the [target/classes] folder, preserving the package hierarchy ([target/classes/first/Main.class]). The [mvn compile exec:java] command compiles the project and then runs it; with the [-q] option, only the program’s output is displayed:
Once the project is compiled, you can also run the class directly using the command [java], specifying the location of the classes:
The [target] folder can be safely deleted ([mvn clean]): it will be recreated during the next compilation. The command [mvn package] also produces the archive [target/first-program-1.0.jar], which can be executed using [java -cp target/first-program-1.0.jar first.Main]: this is the format in which a Java program is delivered.
2.6. A project with multiple modules
2.6.1. The aggregator project
A Maven project can group together several other projects, called its modules. The [02-installation] folder contains a file named [pom.xml] that groups together its two projects:
- Line 11: This project does not produce an archive [.jar]: It serves only to group other projects ([pom]);
- Lines 13–16: the modules, identified by their folder names.
In the [02-installation] folder, the [mvn compile] command compiles both projects; a IDE command that opens this folder sees both projects at once. In Chapter 6, we’ll see that a parent project can also pass a common configuration (library versions, compiler options) to its modules.
2.6.2. A project with multiple main methods
A project can contain multiple classes, each with a [main] method: each is a possible entry point, and the class to be executed is chosen at runtime. The project [TwoHands] contains two classes. The [P1] class:
and the class [P2], which is identical except for its name. The command [java] executes the class specified:
For the command [mvn exec:java], the project file [pom.xml] specifies the class to be executed:
- Line 7: The method [main] that is executed belongs to the class [twohands.P2] (the full class name, including the package).
Results:
You can choose a different class without modifying the [pom.xml] file: [mvn -q exec:java -Dexec.mainClass=twohands.P1].
2.7. Examples in the document
To run the examples in this document:
- download the [java-oct-2026] folder (link at the beginning of the document);
- open it in VS Code (“File / Open Folder”), or, for a given Maven project, open its folder in IntelliJ IDEA;
- in a terminal located in a chapter’s folder, run a compact source file using [java Name.java], or a Maven project using [mvn -q compile exec:java] in the project folder (the text of each chapter provides the exact command).
Chapters 10 and 11 use a database named MySQL; the installation of MySQL along with [Laragon] is described in the appendix.