Java Setup Guide - CodeSwordsman

Set up Java: install Temurin/OpenJDK, switch versions with SDKMAN, configure JAVA_HOME, switch Maven and Gradle repositories, scaffold projects and fix common errors.

JDK 17 / 21 LTSUpdated 2026-10-2929 commands

Install & Version Management

Install Temurin 21 LTS on Windows; the installer writes JAVA_HOME automatically

winget install EclipseAdoptium.Temurin.21.JDK

Install JDK 21 with Homebrew on macOS or the Adoptium repo on Ubuntu

# macOS
brew install --cask temurin@21
# Ubuntu / Debian
sudo apt update && sudo apt install -y temurin-21-jdk

Install the JDK with SDKMAN on macOS/Linux so versions can be switched with one command

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
sdk install java 21.0.4-tem

SDKMAN does not officially support Windows; use winget or WSL instead

List available versions, switch for the session and set the default JDK

sdk list java
sdk use java 17.0.12-tem
sdk default java 21.0.4-tem

Install Maven and confirm which JDK it detects

winget install Apache.Maven
mvn -v

Install Gradle; the output also shows the JVM version in use

winget install Gradle.Gradle
# macOS
brew install gradle
gradle -v

Mirrors & Proxy

Add this to the mirrors section of ~/.m2/settings.xml so Maven resolves everything from Aliyun

<mirror>
  <id>aliyun</id>
  <mirrorOf>*</mirrorOf>
  <url>https://maven.aliyun.com/repository/public</url>
</mirror>

Show the merged effective settings to confirm the mirror is loaded

mvn help:effective-settings

Point the repositories block in build.gradle.kts at the Aliyun mirror

repositories {
  maven { url = uri("https://maven.aliyun.com/repository/public") }
}

Temporarily skip SSL verification for self-signed corporate certificates; intranet only

mvn -Dmaven.wagon.http.ssl.insecure=true clean package

Route the JVM through a proxy to debug dependency downloads and outbound timeouts

java -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7890 -jar app.jar

Environment Variables

Set JAVA_HOME on Windows and append its bin directory to PATH

# Windows(永久生效,需重开终端)
setx JAVA_HOME "C:\Program Files\Eclipse Adoptium\jdk-21"
setx PATH "%JAVA_HOME%\bin;%PATH%"

Never overwrite PATH; append to the existing value or system commands break

Resolve the JDK with java_home on macOS, then persist it in the shell profile

# macOS / Linux
echo 'export JAVA_HOME=$(/usr/libexec/java_home -v 21)' >> ~/.zshrc
echo 'export PATH="$JAVA_HOME/bin:$PATH"' >> ~/.zshrc

Confirm the effective JAVA_HOME first when multiple JDKs conflict

echo %JAVA_HOME%
# macOS / Linux
echo $JAVA_HOME

Maven prints both the Java version and JAVA_HOME, the quickest consistency check

mvn -version

Create & Verify

Verify the runtime and compiler match; mismatches mean PATH points at another JDK

java -version
javac -version

Generate a project with the standard Maven layout from an archetype

mvn archetype:generate -DgroupId=com.demo -DartifactId=demo -DinteractiveMode=false

Package and run the artifact to verify the whole build chain

mvn clean package
java -jar target/demo-1.0-SNAPSHOT.jar

Initialize a Java application project with Gradle

gradle init --type java-application

Open the built-in interactive shell to quickly verify syntax and API behavior

jshell

IDE & Plugins

Install the Java extension pack (debug, test, dependency tooling) and the language server in VS Code

code --install-extension vscjava.vscode-java-pack
code --install-extension redhat.java

Install the free IntelliJ IDEA Community edition on Windows; use brew --cask on macOS

winget install JetBrains.IntelliJIDEA.Community

Manage Maven and Gradle versions with SDKMAN so the team stays consistent

sdk install maven
sdk install gradle

Troubleshooting

With several JDKs installed, check which one the command actually resolves to

where java
# macOS / Linux
which -a java

Print the JVM java.home to catch a JAVA_HOME pointing at the wrong directory

java -XshowSettings:properties -version 2>&1 | findstr java.home

Force a refresh of SNAPSHOT dependencies from the remote repository

mvn -U clean install

Trace dependency conflicts to find multiple versions of the same library

mvn dependency:tree -Dincludes=org.slf4j
Risky

Delete the corrupted artifact directory and re-resolve it

rm -rf ~/.m2/repository/com/demo

Check the default max heap when startup fails in a memory-limited container

java -XX:+PrintFlagsFinal -version | findstr MaxHeapSize

This website uses cookies to ensure you get the best experience on our website. Privacy Policy.