Java 18: Build a Simple HTTP Server in Minutes
- Published on
- Authors
- Name
- Spaghetti Code Jungle
- @spagcodejungle

Java 18: Build a Simple HTTP Server in Minutes
🚀 Introduction
Java 18 introduced a surprisingly handy feature that flew under the radar: a built-in HTTP server. No need for Spring Boot, Jetty, or any external libraries.
Whether you're a beginner learning how HTTP works or a senior dev testing frontend files, this feature gives you a fast, lightweight solution right out of the box.
🤔 Why This Matters
Setting up a web server in Java used to mean a lot of boilerplate or heavy frameworks.
But now? You can:
- ⚡ Serve static files with one line of terminal code
- 🎯 Quickly test frontend integrations
- 🧪 Spin up mock servers or simple APIs without any setup
This is perfect for:
- Beginners learning HTTP and web concepts
- Senior developers looking for lightweight, disposable test servers
- Hackers and tinkerers building tools, demos, or prototypes
🛠️ Getting Started
✅ Prerequisites
- Java 18 installed
- Basic familiarity with the terminal and Java code
🔧 Running the Simple HTTP Server (No Code Required)
Java 18 comes with a built-in CLI tool called jwebserver.
To serve files from your current directory, run:
jwebserver --directory . --port 8080
What These Flags Mean:
--directory: The folder containing the files to serve--port: The port number you want the server to run on (default is 8000 if omitted)
💡 Example in Action
Place an
index.htmlfile in your project folderOpen your terminal and run:
jwebserver --directory . --port 8080Visit http://localhost:8080 in your browser
🎉 You’ll see your page instantly — no extra config needed
💻 Serving Custom Responses with Java Code
If you want more than static file serving — like a basic API — you can still do that with a few lines of code using Java's built-in HTTP server classes.
Here’s a minimal example:
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class SimpleServer {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/", new HelloHandler());
server.start();
System.out.println("Server running on http://localhost:8080");
}
static class HelloHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
String response = "Hello from Java 18!";
exchange.sendResponseHeaders(200, response.length());
OutputStream os = exchange.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
🧪 This sets up a simple HTTP server that responds with Hello from Java 18! to any request on port 8080.
🧰 Use Cases
You can use this simple HTTP server for:
- 🖼️ Serving static websites locally
- 🧪 Mocking API endpoints
- 💼 Internal tools or dashboards
- ⚡ Quick demonstrations without framework overhead
- 🧠 Learning and experimentation
⚠️ Limitations
This server is not meant for production. Here’s why:
- ❌ No HTTPS support
- ❌ No advanced routing or middleware
- ❌ Limited customization (which is also what makes it fast!)
✅ Conclusion
Java 18 makes it easier than ever to run a simple HTTP server with:
- ⚡ One terminal command or a few lines of code
- 🧪 No frameworks
- 🧰 Built-in tools
- 🚀 Instant results
Whether you're a junior dev learning the ropes or a seasoned pro in need of a fast solution, this tool is a low-friction way to get started.