I recommend watching first – installing Homebrew and asdf on Ubuntu (it’s short, just 5 commands)
📘 Official Documentation
Java – Official Documentation
Java – On DevDocs.io
⭐ Popular Frameworks (ordered from lower to higher learning curve)
- Spark Java — Minimalist, lightweight.
- Javalin — Simple, modern.
- Spring Boot — Complete, enterprise.
🛠️ Installing Java on Ubuntu
sudo apt update
sudo apt install openjdk-21-jdk
Verify installation:
java --version
javac --version
🍺 Installation with Homebrew
brew install openjdk
Add Java to PATH (if brew indicates it):
echo 'export PATH="/home/linuxbrew/.linuxbrew/opt/openjdk/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
📦 Standard Dependency Manager (Maven / Gradle)
Check Maven version:
mvn --version
Check Gradle version:
gradle --version
🔧 Installation with ASDF
System dependencies
sudo apt update
sudo apt install -y curl git unzip zip
Install plugin + version
asdf plugin add java
asdf list-all java
# install different versions
asdf install java openjdk-21.0.2
asdf install java temurin-17.0.10
# Set a global version
asdf global java openjdk-21.0.2
# Set a local version
asdf local java temurin-17.0.10
Example: .tool-versions
java openjdk-21.0.2
📝▶️ Create and run a Java file
Create file:
touch Hola.java
Content of Hola.java
public class Hola {
public static void main(String[] args) {
System.out.println("Hello World from Java!");
}
}
💻 Compile and run:
javac Hola.java
java Hola
🟦 Basic Java example
🗂️🌐 Static file Web Server
**Note:* Java includes a basic HTTP server (HttpServer). You don’t need frameworks.*
What it does:
Defines the query parameter name
Gets the value from the URL
- stripHtmlTags(text): removes HTML tags
- Renders the received variable inside an
<h1>
📝 Create file: touch WebServer.java
📦 Content of WebServer.java
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
public class WebServer {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(7000), 0);
server.createContext("/", (HttpExchange exchange) -> {
String query = exchange.getRequestURI().getQuery();
String username = "guest";
if (query != null && query.startsWith("username=")) {
username = URLDecoder.decode(
query.replace("username=", ""),
StandardCharsets.UTF_8
);
username = stripHtmlTags(username);
}
String response = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello</title>
</head>
<body style="text-align:center">
<h1>Hello, %s</h1>
</body>
</html>
""".formatted(username);
exchange.getResponseHeaders().add("Content-Type", "text/html");
exchange.sendResponseHeaders(200, response.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
});
server.start();
System.out.println("Server running at http://localhost:7000");
System.out.println("Try at http://localhost:7000?username=Homer");
}
private static String stripHtmlTags(String input) {
return input.replaceAll("<[^>]*>", "");
}
}
▶️ Compile and run
javac WebServer.java
java WebServer
👉 visit:
http://localhost:7000/?username=Homer
⚙️🧩 JSON REST API
What it does:
- Reads data from a
data.jsonfile - Exposes 2 endpoints:
- Character list at
/characters - Character by id at
/characters/:id
Example file: data.json
[
{
"id": 1,
"age": 39,
"name": "Homer Tompson",
"portrait_path": "https://cdn.thesimpsonsapi.com/500/character/1.webp"
},
{
"id": 2,
"age": 39,
"name": "Marge Simpson",
"portrait_path": "https://cdn.thesimpsonsapi.com/500/character/2.webp"
}
]
📝 Create file: touch ApiServer.java
▶️ File content: ApiServer.java
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
public class ApiServer {
private static String jsonData;
public static void main(String[] args) throws Exception {
jsonData = Files.readString(Path.of("data.json"));
HttpServer server = HttpServer.create(new InetSocketAddress(7001), 0);
server.createContext("/characters", (HttpExchange exchange) -> {
String path = exchange.getRequestURI().getPath();
exchange.getResponseHeaders().add("Content-Type", "application/json");
// GET /characters
if (path.equals("/characters")) {
send(exchange, 200, jsonData);
return;
}
// GET /characters/:id
if (path.startsWith("/characters/")) {
String id = path.replace("/characters/", "");
if (jsonData.contains("\"id\": " + id)) {
send(exchange, 200, jsonData);
} else {
send(exchange, 404, "{\"error\":\"Character not found\"}");
}
return;
}
send(exchange, 404, "{\"error\":\"Route not found\"}");
});
server.start();
System.out.println("Server running at http://localhost:7001/characters");
}
private static void send(HttpExchange exchange, int status, String body) throws IOException {
exchange.sendResponseHeaders(status, body.getBytes().length);
OutputStream os = exchange.getResponseBody();
os.write(body.getBytes());
os.close();
}
}
▶️ Compile and run
javac ApiServer.java
java ApiServer
👉 visit:
http://localhost:7001/characters
To test the endpoint by id:
http://localhost:7001/characters/1

Top comments (0)