The Java Buildpack can run applications which provide their own web server or servlet container, provided as JAR files.
This example uses Jetty as an embedded web server, but should be applicable for other technologies.
publicstaticvoidmain(String[] args) {
intport = Integer.parseInt(System.getenv("PORT"));
Serverserver = newServer(port);
server.setHandler(newAbstractHandler() {
@Overridepublicvoidhandle(Stringtarget, RequestbaseRequest, HttpServletRequestrequest, HttpServletResponseresponse) throwsIOException, ServletException {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("<h1>Hello, world</h1>");
}
});
server.start();
server.join();
}The important takeaway is to note that the port comes from the environment variable port. Other variables are detailed in the Cloud Foundry developer guide. When the application is built as an executable JAR file, it will be treated as a Java Main application.