JEP 408: Simple Web Server with JBang

1 minute read

In March JEP: 408: Simple Web Server was announced. One of JEP 408’s goals is to make an easy to use web server available with Java using java -m jdk.httpserver.

It is a nice idea and I’m looking forward to see it happen.

That said - you do not need to wait for Java 18+ to get this. You can use jbang httpd@jbangdev today with Java 1.8 and upwards.

It is a script I wrote in a few hours after seeing yet another tweet asking for a simple to use webserver. Like JEP 408 it only uses classes available in the JDK, meaning zero additonal download and setup besides installing jbang.

By default, httpd@jbangdev will serve the current directory on https://localhost:8000.

Below is an example of using httpd@jbangdev to serve this website locally from its statically generated Jekyll _site folder on port 8080.

$ jbang httpd@jbangdev -p 8080 -d _site
[jbang] Building jar...
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) from /Users/max/code/jbangdev/jbang-catalog/_site ...
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/css/main.css" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/js/main.min.js" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/images/logo.png" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/images/carbon-deps.png" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/images/carbon-install.png" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/images/carbon-java.png" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/images/feature-ide.png" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/images/feature-version-juggle.svg" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/images/feature-appstore.png" 200 -
127.0.0.1 - - [04/Jun/2021:00:50:36 +0200] "GET /assets/images/slider/bg-1.jpg" 200 -

As with any other jbang capable application you can also choose to install it in your PATH using jbang app install http@jbangdev. Once you do that you can run it from anywhere using just httpd.

In case you already have application called httpd you can use --name to give it a more unique name:

$ jbang app install --name jhttpd httpd@jbangdev
[jbang] Command installed: jhttpd
$ jhttpd
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) from /Users/max/code/personal/jbangdev/jbang.dev ...

Enjoy - and remember you can find httpd@jbangdev and other apps in JBang AppStore.

Leave a comment