Step-By-Step Process To Building Java application
- Java code: I have a simple Java application that will print “Hello World” on the screen. We can create your Java project.
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
public class HelloWorld {
public static void main(String[] args) throws IOException {
// Create a new HTTP server listening on port 8001
HttpServer server = HttpServer.create(new InetSocketAddress(8001), 0);
// Create a context for handling requests
server.createContext("/", new HttpHandler() {
@Override
public void handle(HttpExchange exchange) throws IOException {
// Set the response headers
exchange.getResponseHeaders().set("Content-Type", "text/html");
exchange.sendResponseHeaders(200, 0);
// Get the response body stream
OutputStream responseBody = exchange.getResponseBody();
// Write the HTML content to the response body
String htmlResponse = "<html><body><h1>Hello, World!</h1></body></html>";
responseBody.write(htmlResponse.getBytes());
// Close the response body stream
responseBody.close();
}
});
// Start the server
server.start();
// Output a message indicating the server has started
System.out.println("Server is listening on port 8001...");
}
}
Now, Before deploying this application with the Docker we have to build it with the help of Dockerfile.
Build And Deploy Java Application With Docker | Step-By-Step Tutorial
Docker is an OS-level virtualization that helps to build and deploy any program. Docker is used to utilize the resources, and it is compatible with all operating systems.