Skip to content

Instantly share code, notes, and snippets.

@igm
Created October 10, 2014 09:58
Show Gist options
  • Save igm/a92748aa7fa01e4388f5 to your computer and use it in GitHub Desktop.
Save igm/a92748aa7fa01e4388f5 to your computer and use it in GitHub Desktop.
Java Main Container
package test;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String args[]) {
ServerSocket sock;
String port = System.getenv("VCAP_APP_PORT");
if (port == null) {
port = "8080";
}
try {
sock = new ServerSocket(Integer.parseInt(port));
System.out.println("WebServer started on port: " + port);
while (true) {
Socket socket = sock.accept();
System.out.println("Stdout: New connection accepted "
+ socket.getInetAddress() + ":" + socket.getPort());
System.err.println("StdErr: No problem, just saying...");
try {
OutputStream os = socket.getOutputStream();
os.write("HTTP/1.1 200 OK\n".getBytes());
os.write("Content-Type: text/plain\n".getBytes());
os.write("Connection: close\n".getBytes());
os.write("Content-Length: 2\n".getBytes());
os.write("\n".getBytes());
os.flush();
os.write("ok".getBytes());
System.out.println("\n".getBytes().length);
os.flush();
os.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>test.Main</mainClass>
<packageName>test</packageName>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment