NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.
http://nanohttpd.org
BSD 3-Clause "New" or "Revised" License
6.9k stars 1.69k forks source link

[Error]: Exception in thread "NanoHttpd Main Listener" java.lang.InternalError: Thread starting during runtime shutdown #568

Open Wanyor opened 4 years ago

Wanyor commented 4 years ago

I run nanohttpd on android 9.0, but show some exception.

Exception:

Exception in thread "NanoHttpd Main Listener" java.lang.InternalError: Thread starting during runtime shutdown at java.lang.Thread.nativeCreate(Native Method) at java.lang.Thread.start(Thread.java:753) at fi.iki.elonen.NanoHTTPD$DefaultAsyncRunner.exec(NanoHTTPD.java:376) at fi.iki.elonen.NanoHTTPD$ServerRunnable.run(NanoHTTPD.java:1774) at java.lang.Thread.run(Thread.java:784)

Code: ` import fi.iki.elonen.NanoHTTPD;

import java.io.IOException;

public class TestServer extends NanoHTTPD {

private TestServer(String host, int port)  {
    super(host, port);
    try {
        start();
    } catch (IOException e) {
        System.out.println("Server Stop: " + e.getMessage());
    }
    System.out.println("Server Running! Point your browsers to http://localhost:8888/ \n");
}

private TestServer(int port){
    super(port);
    try {
        start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
    } catch (IOException e) {
        System.out.println("Server Stop: " + e.getMessage());
    }
    System.out.println("Server Running! Point your browsers to http://localhost:8888/ \n");
}

@Override
public Response serve(IHTTPSession session) {
    System.out.println(session.getUri());
    String msg =
            "<html>" +
                "<body>" +
                    "<h1>Nanohttpd</h1>" +
                "</body>" +
            "</html>";
    return newFixedLengthResponse(msg);
}

public static void main(String[] args) {
    try {
        new TestServer(8888);
    } catch (Throwable e) {
        System.out.println("Error:" +  e.getMessage());
    }
}

} `

byteg commented 4 years ago

Having the same issue. Any progress on this?

byteg commented 4 years ago

In case anyone faces this issue: the problem is that you exit main thread just after starting the server. Solution for console apps is classic - wait for user input or make an infinite loop.

public static void main(String[] args) {
        System.out.println("Hellow from server");
        try {
            new Server();

            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 
            String name = reader.readLine(); 
        } catch (IOException ioe) {
            System.err.println("Couldn't start server:\n" + ioe);
        }

    }