Skip to main content

Deploy Java Apps

Java installation is powered by Adoptium Project. To install latest version with deployment script:

features:
- java

Without this, java won't be available since no globally installed Java runtime available.

Install other version using java latest, java lts, java 18, etc. To uninstall use java off. Check the current version using java --version from SSH.

Example

The deployment script below installs the latest java compiler and writes Main.java and run it directly.

https://github.com/domcloud/recipes/blob/master/java.yml
source: clear
features:
- java
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT java Main.java
commands:
- filename: Main.java
content: |
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class Main {
public static void main(String[] args) throws Exception {
String portStr = System.getenv("PORT");
int port = portStr == null ? 3000 : Integer.parseInt(portStr);
HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
System.out.println("Server is listening to localhost:" + port);
server.createContext("/", new DefaultHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
}

class DefaultHandler implements HttpHandler {
private static String template = """
<!DOCTYPE html>
<html>
<head>
<title>Java App</title>
<link rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css">
</head>
<body class="p-5 text-center">
<p><img src="//images.unsplash.com/photo-1465153690352-10c1b29577f8?fit=crop&w=200&h=200"
class="img-fluid rounded-circle"></p>
<h1 class="mb-3">Hello, world!</h1>
<p>Serving from Java version %s</p>
<p class="text-muted">DOM Cloud</p>
</body>
</html>
""";

public void handle(HttpExchange t) throws IOException {
String version = Runtime.version().toString();
String response = String.format(template, version);
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}

Existing Java projects

Existing Java projects can run directly from source code. This eliminates the need to compile the code into a JAR file, although startup time may be a little bit slower to start.

https://github.com/domcloud/recipes/blob/master/java-gls.yml
features:
- java
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT java Main.java

App Management

Your app do not restarted automatically after file changes. To restart, run restart via SSH.

Environment variables can be set either using NGINX's env_var_list or ~/.bashrc. Usually your language framework also reads .env files.

See NGINX and App Daemon for more information about NGINX and App managements including restarting, environment variables, and other global limitations.

App Logging

You can see app log from Check -> Check Process Logs tab. Only startup problems displayed in the browser.

You might want to use a proper logging mechanism such as log4j then write it to a log file, or any other solution that suits you.

NGINX errors and traffic logs can be examined via Webmin or Check -> Check Process Logs tab.