·
Java 18 will provide a minimally functional web server in the jdk.httpserver module.
It has an API for access, as well as a binary in the bin directory in case you want to run the server from the command line.
The command to run the web server can be as simple as:
$ jwebserver -b 0.0.0.0 -p 8000
If you are wondering whether you can implement a full-blown production web server using the simple web server APIs, the answer is no.
The web server is definitely not intended for that use.
For one thing, it communicates over HTTP/1.1 and doesn't support PUT requests, so it doesn't support dynamic content.
The web server is recommended for prototyping, testing, and debugging. An example code snippet with comments follows:
import java.net.InetSocketAddress; import java.nio.file.Path; import com.sun.net.httpserver.SimpleFileServer; import static com.sun.net.httpserver.SimpleFileServer.OutputLevel; public class App { public static void main( String[] args ){ // Create a simple file server, //given the socket address, path and output level var server = SimpleFileServer.createFileServer( new InetSocketAddress(8000), Path.of("/home/java"), OutputLevel.VERBOSE); // Starting the server server.start(); // A friendly message to get started. System.out.println( "We are getting started.. Hello World!" ); } }