1 package fi.iki.elonen; 2 3 import java.io.IOException; 4 5 public class ServerRunner { 6 public static void run(Class serverClass) { 7 try { 8 executeInstance((NanoHTTPD) serverClass.newInstance()); 9 } catch (Exception e) { 10 e.printStackTrace(); 11 } 12 } 13 14 public static void executeInstance(NanoHTTPD server) { 15 try { 16 server.start(); 17 } catch (IOException ioe) { 18 System.err.println("Couldn't start server:\n" + ioe); 19 System.exit(-1); 20 } 21 22 System.out.println("Server started, Hit Enter to stop.\n"); 23 24 try { 25 System.in.read(); 26 } catch (Throwable ignored) { 27 } 28 29 server.stop(); 30 System.out.println("Server stopped.\n"); 31 } 32 } 33