Skip to main content

Deploy Java Apps

Java is a programming language popularly used globally. Java is served as a compiled language, which means you need to compile your code before running it.

There is no default java compiler at system level. You have to install it first through deployment scripts.

warning

Serving Java apps requires to be run via GLS, that's mean you have to listen from given PORT env/args.

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

Use other Java versions

To switch Java version use feature syntax like java latest, java 18, etc. Check your current Java version using java --version in SSH.

features:
- java 18

Java installation binaries is used from Adoptium Project. Only latest build from them in each major version (as low as jdk 8) is available to download using this feature.

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.

Please 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.