iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Get Started

Install a modern JDK and Maven (or Gradle). Build, run, and test your first Java app.

Java — getting started

EXAMPLE
# ===== 1. Install a JDK =====
# Use a version manager:
#   - sdkman (macOS/Linux): https://sdkman.io
#   - jenv:                 https://www.jenv.be
#   - Windows: use Adoptium / Microsoft Build / GraalVM installers

# sdkman:
curl -s https://get.sdkman.io | bash
# Restart shell, then:
sdk install java 21.0.3-tem        # Temurin LTS
sdk install maven
sdk install gradle

java -version
mvn -version
gradle -version

# ===== 2. Maven project =====
mvn archetype:generate \
  -DgroupId=com.example -DartifactId=hello \
  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
cd hello

# Default project layout:
# src/main/java/com/example/App.java
# src/test/java/com/example/AppTest.java
# pom.xml

# Build + run:
mvn package
java -cp target/hello-1.0-SNAPSHOT.jar com.example.App
# 'Hello World!'

# ===== 3. Modern Java idioms (in App.java) =====
package com.example;

public class App {
    record User(int id, String name) {}

    public static void main(String[] args) {
        var u = new User(1, "Alex");
        System.out.println(u);

        var size = switch (u.id()) {
            case 0 -> "zero";
            case 1, 2, 3 -> "small";
            default -> "other";
        };
        System.out.println(size);
    }
}

# ===== 4. Tests (JUnit 5) =====
# pom.xml dependencies:
#   org.junit.jupiter:junit-jupiter:5.10.x
# AppTest.java:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class AppTest {
    @Test void example() { assertEquals(2, 1 + 1); }
}

mvn test

# ===== 5. Gradle alternative =====
gradle init --type java-application
gradle build
gradle run

# ===== 6. A tiny HTTP server (jdk.httpserver) =====
import com.sun.net.httpserver.*;
import java.net.InetSocketAddress;

HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/healthz", ex -> {
    var body = "{\"ok\":true}".getBytes();
    ex.getResponseHeaders().add("content-type", "application/json");
    ex.sendResponseHeaders(200, body.length);
    ex.getResponseBody().write(body);
    ex.close();
});
server.start();

# ===== Patterns to internalise =====
# - LTS JDK (21+) for production
# - Maven for stable / predictable; Gradle for power + flexibility
# - records for value DTOs; sealed interfaces for closed hierarchies
# - JUnit 5 for tests; do not pull JUnit 4 into new code

# ===== Pitfalls =====
# - System Java differing from project Java -> use a version manager
# - Forgetting -source / -target / --release on the compiler
# - Catching Exception broadly -> swallow real bugs
# - 'Implementation detail' types exposed in public APIs -> brittle

Why it matters

Install a JDK with sdkman, pick Maven or Gradle, scaffold, build, test. Java has the longest stable runtime story in the industry; the cost is some upfront ceremony that pays back in production reliability.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Install a JDK (Temurin / Oracle / Microsoft Build)
javac --version
java --version
javac Main.java && java Main
Try it Yourself »

Discussion

Loading…