Static files
Static files are files the server sends as they are, without processing: HTML pages, CSS styles, images, JavaScript. Next to an API you often need at least a start page or a ready frontend. Javalin serves such files with one configuration line.
Where the files live
Create the src/main/resources/public folder and put two files in it:
<!-- src/main/resources/public/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<h1>Blog</h1>
<p>The API lives at <a href="/posts">/posts</a>.</p>
</body>
</html>/* src/main/resources/public/style.css */
body { font-family: sans-serif; max-width: 40rem; margin: 2rem auto; }Maven copies everything from src/main/resources into target/classes, that is, into the classpath — the set of folders and jar files Java loads classes and resources from. So the files also end up in the final jar.
Enabling it
Add a line to the configuration block:
// src/main/java/com/example/blog/Main.java
config.staticFiles.add("/public", Location.CLASSPATH); // io.javalin.http.staticfiles.LocationThe first argument is the folder inside the classpath, the second is where to look for it. At startup Javalin logs: Static file handler added: StaticFileConfig(hostedPath=/, directory=/public, location=CLASSPATH, ...).
The files are available from the site root: public/style.css opens at /style.css, and for / Javalin serves index.html:
curl -i http://localhost:8080/style.cssHTTP/1.1 200 OK
Content-Type: text/css
Cache-Control: max-age=0
body { font-family: sans-serif; max-width: 40rem; margin: 2rem auto; }
Javalin chose the content type from the file extension. It also adds an ETag header (omitted here): the browser uses it to tell whether the file has changed. Open http://localhost:8080 in a browser — you see the page with a link to the API.
Processing order
Javalin first looks for a route, and only if there is none does it look for a file. So the config.routes.get("/", ...) route from the first lesson hides index.html. Remove it so that / opens the page.
Settings
For finer settings there is a variant with a lambda:
| Property | What it sets |
|---|---|
hostedPath | The address the files are served from, e.g. /assets |
directory | The folder with the files |
location | Location.CLASSPATH (inside the jar) or Location.EXTERNAL (a folder on disk) |
headers | Headers for all files, e.g. caching |
// src/main/java/com/example/blog/Main.java
config.staticFiles.add(files -> {
files.hostedPath = "/assets";
files.directory = "/public";
files.location = Location.CLASSPATH;
});With these settings the style is available at /assets/style.css.
Common mistake
If the given folder is not in the classpath, the server does not start:
Exception in thread "main" io.javalin.util.JavalinException: Static resource directory with path: '/static' does not exist. Depending on your setup, empty folders might not get copied to classpath.
Check that the folder name in the code matches the folder in src/main/resources and that it contains at least one file: Maven does not copy empty folders.