Java Vanilla

Complete deployment guide for Java Vanilla on NStrim

Overview

Java Vanilla is a plain Java project using only the standard library and a lightweight HTTP server. It provides a clean starting point for building web applications and REST APIs without any framework overhead.

Before You Start
Make sure you have your dashboard open - you'll need the Git credentials displayed there. This template includes a lightweight HTTP server, JDBC for database access, and Gson for JSON handling.

Quick Start

1

Create Repository from Template

On your dashboard, go to the Tools & Deployment tab. Click the "New Repository" button, select the Java Vanilla template from the dropdown, enter a repository name and subdomain, then click "Create Repository".

2

Clone Your Repository

git clone https://<git-server>/<username>/<repo-name>.git
cd <repo-name>
3

Build the Project

mvn clean package
4

Run the Application

java -jar target/app.jar

Server runs on http://localhost:8080 by default.

5

Push Your Changes

git add .
git commit -m "Initial setup"
git push origin main
Every push to Git automatically deploys your application. Check your Module Work URL on the dashboard to see your live site.

Local Development with Docker

If you don't have Java or Maven installed locally, you can use Docker to build and run your project.

What is Docker?
Docker creates isolated "containers" with all the software you need (Java, Maven) without installing them on your computer.

Running Commands with Docker

# Build the project
docker run --rm -v $(pwd):/app -w /app maven:3-eclipse-temurin-21 mvn clean package

# Run the application
docker run --rm -v $(pwd):/app -w /app -p 8080:8080 eclipse-temurin:21 java -jar target/app.jar

Project Structure

├── src/
│   └── main/
│       └── java/
│           └── com/example/
│               ├── App.java              # Entry point & HTTP server
│               ├── handlers/             # Request handlers
│               └── models/               # Data models
├── pom.xml                               # Maven dependencies
└── Dockerfile                            # Docker config
        

Common Tasks

Creating an HTTP Handler

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.io.OutputStream;

public class UsersHandler implements HttpHandler {
    @Override
    public void handle(HttpExchange exchange) throws IOException {
        String response = "{\"message\": \"Hello from Java!\"}";
        exchange.getResponseHeaders().set("Content-Type", "application/json");
        exchange.sendResponseHeaders(200, response.length());
        try (OutputStream os = exchange.getResponseBody()) {
            os.write(response.getBytes());
        }
    }
}

Database Connection with JDBC

import java.sql.*;

String url = "jdbc:mysql://db.<session-domain>:3306/<username>";
Connection conn = DriverManager.getConnection(url, "username", "password");

PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE id = ?");
stmt.setInt(1, userId);
ResultSet rs = stmt.executeQuery();

Useful Commands

mvn clean package Build the project
java -jar target/app.jar Run the application
mvn dependency:tree Show dependency tree

Troubleshooting

java.sql.SQLException: Access denied

Solution: Check your JDBC connection URL, username, and password match the credentials from your dashboard.

Port 8080 already in use

Solution: Change the port in your App.java or stop the other process using port 8080.

ClassNotFoundException for JDBC driver

Solution: Ensure the MySQL JDBC driver is included in your pom.xml dependencies and run mvn clean package again.

Deployment Workflow

Code Change git push mvn package Live!
For more information, visit the official Java Documentation.