Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 119 ยท Getting Started

Setting Up a Java Development Environment

Set up a professional Java workstation - choose a JDK distribution, set JAVA_HOME, pick an IDE, learn the standard project layout and run databases in Docker.

The examples in this course run on our server, but real projects run on your machine first. A professional Java setup has four parts: a JDK to compile and run code, an IDE to write and debug it, a standard project layout that every build tool and teammate understands, and a way to run supporting services such as databases locally, which today almost always means Docker.

None of this is difficult, but small mistakes here (two JDKs fighting over PATH, a config file in the wrong folder, a database password committed to Git) cost hours later. This lesson shows the choices professionals make and why, and uses runnable examples to show what the JVM sees at runtime: its version, its environment and its classpath.

Choosing a JDK

The JDK (Java Development Kit) contains the compiler (javac), the JVM, the standard library and tools such as jar, jshell, jcmd and jlink. How Java Works explains the parts. What surprises newcomers is that there is no single "official" JDK download. Almost all JDKs are built from the same open-source code, OpenJDK, and differ mainly in license, support and update policy.

Distribution Vendor Notes
Eclipse Temurin Adoptium (Eclipse Foundation) Free, TCK-certified, the usual default choice. The runner on this site uses it.
Oracle OpenJDK builds Oracle Free (GPL), but each release only gets updates for six months.
Oracle JDK Oracle Free to use under Oracle's own license for current LTS releases for a limited period; paid support after that. Read the license before using it at work.
Amazon Corretto Amazon Free, long-term updates, used across AWS.
Microsoft Build of OpenJDK Microsoft Free, used across Azure.
Azul Zulu Azul Free builds, paid support available; many platforms.
BellSoft Liberica BellSoft Free; offers small builds popular in containers.
GraalVM Oracle A JDK with an extra compiler that can build native executables. Covered in Deploying Java Applications.

For learning and for most companies, Temurin is a safe, neutral default. If your company deploys to AWS or Azure, the cloud vendor's build is equally good. The important rule is to use the same major version locally as in production.

Release cadence and LTS

A new Java version ships every six months, in March and September. Every two years one release is marked LTS (long-term support): Java 17 (2021), 21 (2023) and 25 (2025). Vendors update LTS releases for many years, while the releases between them (22, 23, 24) are replaced six months later. Companies almost always run an LTS in production, so this course targets Java 25, and points out when a feature you use does not exist in 17 or 21. Java Versions and History lists what each release added.

Installing and switching JDKs

You can install a JDK with an installer from the vendor's website, but a package manager is easier to update and lets you keep several versions side by side:

Terminal
# macOS and Linux: SDKMAN! manages many JDKs per user
sdk list java                  # shows every available build
sdk install java 25.0.1-tem    # use an identifier from the list
sdk use java 21.0.8-tem        # switch for this shell only

# Windows: winget
winget install EclipseAdoptium.Temurin.25.JDK

# macOS: Homebrew (latest Temurin)
brew install --cask temurin

Then check what you got. The first command runs programs; the second compiles them, and both should report the same version:

Terminal
java -version
javac -version

Typical output looks like this (the exact update number will differ):

TEXT
openjdk version "25.0.1" 2025-10-21 LTS
OpenJDK Runtime Environment Temurin-25.0.1+8 (build 25.0.1+8-LTS)
OpenJDK 64-Bit Server VM Temurin-25.0.1+8 (build 25.0.1+8-LTS, mixed mode, sharing)

A running program can ask the same questions. This is useful in logs: when a bug only happens on one server, the first thing to check is which JVM it ran on.

Java Which JVM is running this code?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

You should see Java 25 from Eclipse Adoptium (that is, Temurin) running on Linux. The default charset is UTF-8: since Java 18 that is the default on every operating system, which ended a whole class of "works on my machine" bugs with accented characters.

JAVA_HOME and PATH

Two environment variables decide which JDK your tools use:

  • PATH decides which java and javac your terminal finds. The first matching folder wins.
  • JAVA_HOME points to the JDK's root folder (the one that contains bin). Maven, Gradle, application servers and many scripts read it instead of PATH.

If they point to different JDKs, you get confusing errors such as "release version 25 not supported" while java -version says 25. Set both together:

Terminal
# macOS / Linux (add to ~/.bashrc or ~/.zshrc; SDKMAN! does this for you)
export JAVA_HOME="$HOME/.sdkman/candidates/java/current"
export PATH="$JAVA_HOME/bin:$PATH"

# Windows (PowerShell, current user; open a new terminal afterwards)
setx JAVA_HOME "C:\Program Files\Eclipse Adoptium\jdk-25.0.1.8-hotspot"
# then add %JAVA_HOME%\bin to Path in "Edit environment variables for your account"

Check for stowaways. Old JDKs, database tools and some installers put their own java early on PATH. On Windows, run where java; on macOS and Linux, which -a java. The first line is the one your terminal uses.

Build tools are increasingly independent of both variables. Maven's compiler plugin and Gradle's toolchains can download or select the exact JDK a project declares, which you will see in Maven in Depth and Gradle in Depth.

Picking an IDE

You can write Java in any editor, but a Java-aware IDE pays for itself on day one: it compiles as you type, refactors safely across hundreds of files, runs single tests with one click and has a real debugger (Debugging Java Programs shows how to use one).

  • IntelliJ IDEA (JetBrains) is the most widely used Java IDE. Core Java, Maven, Gradle, Git and JUnit support are free; a paid subscription unlocks the "Ultimate" features such as Spring, Jakarta EE, database and HTTP tooling. Most professional Java teams use it.
  • Visual Studio Code with Microsoft's Extension Pack for Java is a lighter option, good if you also work in other languages. Its Java support comes from the same compiler technology as Eclipse.
  • Eclipse IDE (the "Enterprise Java and Web Developers" package) is free, open source and still common in large companies. Spring Tools adds Spring Boot support to Eclipse and VS Code.

Whichever you choose, learn three things early: how to import a Maven or Gradle project (open the folder with the build file, not individual source files), how to run and debug a single test, and how to make the IDE use the project's JDK version rather than whatever it was installed with.

The standard project layout

Almost every Java project, whether built with Maven or Gradle, uses the same folder structure. It started as Maven's standard directory layout, and Gradle adopted it as its default. When you open an unfamiliar project, you know where everything is:

TEXT
shop-service/
โ”œโ”€โ”€ pom.xml                         (or build.gradle.kts + settings.gradle.kts)
โ”œโ”€โ”€ mvnw, mvnw.cmd, .mvn/           (the build tool wrapper; see the next lessons)
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main/
โ”‚   โ”‚   โ”œโ”€โ”€ java/
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ com/example/shop/
โ”‚   โ”‚   โ”‚       โ”œโ”€โ”€ ShopApplication.java
โ”‚   โ”‚   โ”‚       โ””โ”€โ”€ product/
โ”‚   โ”‚   โ”‚           โ”œโ”€โ”€ Product.java
โ”‚   โ”‚   โ”‚           โ””โ”€โ”€ ProductRepository.java
โ”‚   โ”‚   โ””โ”€โ”€ resources/
โ”‚   โ”‚       โ”œโ”€โ”€ application.properties
โ”‚   โ”‚       โ””โ”€โ”€ db/schema.sql
โ”‚   โ””โ”€โ”€ test/
โ”‚       โ”œโ”€โ”€ java/
โ”‚       โ”‚   โ””โ”€โ”€ com/example/shop/product/
โ”‚       โ”‚       โ””โ”€โ”€ ProductRepositoryTest.java
โ”‚       โ””โ”€โ”€ resources/
โ”‚           โ””โ”€โ”€ test-data.sql
โ”œโ”€โ”€ target/                          (Maven output; Gradle uses build/)
โ””โ”€โ”€ .gitignore

The rules behind it:

  • src/main/java holds production code; src/test/java holds tests. Tests are compiled separately and never end up in the JAR you ship.
  • Folders below java/ mirror packages. com/example/shop/product/Product.java must start with package com.example.shop.product; (Packages and Imports). Use a reversed domain you control as the root.
  • resources holds non-code files: configuration, SQL scripts, templates, messages. The build copies them onto the classpath next to the compiled classes, so code loads them by classpath name, never by file path.
  • target/ or build/ is generated output. Never edit it and never commit it; add it to .gitignore together with IDE folders such as .idea/.

Loading files from the classpath

Because resources end up inside the JAR, new File("src/main/resources/application.properties") works in your IDE and fails in production. The correct way is to ask the class loader, which looks inside every folder and JAR on the classpath. The libraries on this site's classpath contain resources too. For example, the H2 JAR ships a file that tells JDBC which driver class it contains (you will learn why in JDBC Introduction):

Java Reading a resource from the classpath

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The resource is found inside h2-2.5.252.jar and contains one line, org.h2.Driver. The last line prints false, because this sandbox has no application.properties. In a real project, getResourceAsStream("application.properties") would find the file you put in src/main/resources, whether the app runs from the IDE, from target/classes or from a packaged JAR.

Environment variables and configuration

Code is the same everywhere; configuration is not. Your laptop uses a local database, the test server another one, production a third with a secret password. The widely used rule (from the "twelve-factor app" guidelines) is: keep configuration out of the code and read it from the environment. Never commit passwords; give each environment its own values through environment variables, system properties (java -Dkey=value) or a config file that is not in Git.

Java reads them with two calls:

  • System.getenv("DB_URL") returns an environment variable, or null.
  • System.getProperty("db.url") returns a system property set with -Ddb.url=..., or null.

A common pattern combines a defaults file with overrides, where more specific sources win. Spring Boot does exactly this with a long, documented precedence list (Configuration, Properties and Profiles). Here is the idea in plain Java. The sandbox has no custom environment variables, so the example also passes a small map that plays the role of the environment on a production server:

Java Defaults, environment and system properties

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

db.url and db.pool.size come from the "server" environment, db.user falls back to the defaults file, and app.greeting comes from the system property, which beats everything. The last line shows the few variables that really exist in the sandbox, such as HOME and PATH.

Keep secrets out of Git. Commit a .env.example or a documented defaults file with safe local values, and supply real passwords through environment variables or a secret manager. Once a secret is pushed, assume it is leaked and rotate it, even if you delete the commit.

Running databases with Docker

Your application needs a database, and installing PostgreSQL or MySQL directly on your laptop leads to version mismatches and leftover services. Docker runs each service in an isolated container from an official image, at the exact version production uses, and removes it cleanly afterwards. The Docker for Java Developers lesson covers images and containers in depth; for local databases you need only a few commands:

Terminal
# Start PostgreSQL 18 in the background, reachable on localhost:5432
docker run -d --name shop-db \
  -e POSTGRES_DB=shop -e POSTGRES_USER=shop -e POSTGRES_PASSWORD=dev-only-password \
  -p 5432:5432 -v shop-db-data:/var/lib/postgresql \
  postgres:18

docker ps                 # is it running?
docker logs -f shop-db    # watch the startup log
docker stop shop-db       # stop it (data stays in the shop-db-data volume)
docker start shop-db      # start it again

Teams usually check a compose.yaml file into the repository instead, so everybody gets the same services with one command, docker compose up -d:

YAML
services:
  db:
    image: postgres:18
    environment:
      POSTGRES_DB: shop
      POSTGRES_USER: shop
      POSTGRES_PASSWORD: dev-only-password
    ports:
      - "5432:5432"
    volumes:
      - db-data:/var/lib/postgresql

  mysql:
    image: mysql:8.4
    environment:
      MYSQL_DATABASE: shop
      MYSQL_USER: shop
      MYSQL_PASSWORD: dev-only-password
      MYSQL_ROOT_PASSWORD: dev-only-root
    ports:
      - "3306:3306"

volumes:
  db-data:

Your Java code then connects with a URL like jdbc:postgresql://localhost:5432/shop. Combine that with the configuration pattern above, and the same code runs against Docker on your laptop, a managed database in production and an in-memory H2 database in fast tests. Only the URL changes:

Java One code path, many databases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

With no DB_URL set, the program falls back to H2 and prints its version and the URL jdbc:h2:mem:dev. The user name prints as SA, because H2 stores unquoted names in upper case. With the PostgreSQL driver on the classpath and DB_URL pointing at the container, the very same code would print "Connected to PostgreSQL 18...".

A checklist for a new machine

  1. Install an LTS JDK (Temurin 25 is a good default) with a package manager; confirm java -version and javac -version agree.
  2. Set JAVA_HOME to the same JDK and make sure its bin folder is first on PATH.
  3. Install an IDE and point it at that JDK. Open projects through their build file.
  4. Install Git and Docker Desktop (or Docker Engine on Linux). Start local databases with docker compose up -d.
  5. Never commit target/, build/, IDE folders or secrets.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. java -version prints 25, but Maven fails with "release version 25 not supported". What is the most likely cause?

    Your terminal uses PATH, while Maven uses JAVA_HOME. When they point to different JDKs, the build runs on the older one.
  2. 2. Your code reads new File("src/main/resources/sql/schema.sql"). It works in the IDE. What happens when the app runs from its packaged JAR on a server?

    Resources are copied onto the classpath (into the JAR). There is no src folder at runtime, so load them through the class loader by classpath name.
  3. 3. In the configuration example, DB_URL is set in the environment and the program is started with -Ddb.url=jdbc:h2:mem:test. Which URL is used?

    The resolve method checks system properties first, then environment variables, then the defaults file. Spring Boot uses the same order for these sources.
  4. 4. Why does almost every company run an LTS Java release in production?

    The difference is support length. A non-LTS release stops getting security updates when the next version ships six months later.

Summary

  • Almost every JDK is built from OpenJDK; distributions differ in license and update policy. Temurin is a safe default. Use the same major version locally as in production, normally an LTS (17, 21, 25).
  • Install JDKs with a package manager (SDKMAN!, winget, Homebrew) and keep JAVA_HOME and PATH pointing to the same one.
  • IntelliJ IDEA, VS Code and Eclipse all work well. Open projects through their build file and let the IDE use the project's JDK.
  • The standard layout is src/main/java, src/main/resources, src/test/java, src/test/resources, with output in target/ or build/. Load resources from the classpath, never by file path.
  • Read configuration from the environment (System.getenv, -D system properties), keep secrets out of Git, and run local databases in Docker so only the JDBC URL changes between environments.

Next: Maven in Depth, the build tool that downloads your libraries, compiles and tests your code and packages it for production.