Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve java.lang.IllegalStateException: server in wrong state Exception

I am trying to deploy restful web-service in core java project (swing application). I am using jersy for that. I have searched to many site in google but i can't found why this append.

public class Main { 
public static void main(String[] args) throws Exception{        
    ResourceConfig resourceConfig = new ResourceConfig(MyResource.class);
    URI baseUri = UriBuilder.fromUri("http://localhost/").port(10049).build();

    HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri,resourceConfig);
    server.start();
    System.out.println("Press Enter to stop the server. ");
    System.in.read();
    server.stop(0);
}
}


@Path("/hello")
public class MyResource {

// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
    return "Hello Jersey";
}

// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
    return "sdasd";
}

// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
    return "dsdfd";
}
}

Exception :

 Exception in thread "main" java.lang.IllegalStateException: server in wrong state
at sun.net.httpserver.ServerImpl.start(ServerImpl.java:139)
at sun.net.httpserver.HttpServerImpl.start(HttpServerImpl.java:58)
at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory$2.start(JdkHttpServerFactory.java:363)

Maven:

<dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jdk-http</artifactId>
        <version>2.22.1</version>
    </dependency>

How to resolve above exception.

like image 659
Kamlesh Kanazariya Avatar asked Dec 24 '22 09:12

Kamlesh Kanazariya


1 Answers

This issue is resolved by removing line server.start();

Line JdkHttpServerFactory.createHttpServer(baseUri,resourceConfig); is creating http server as well as start server.

"server in wrong state Exception" comes when you are trying to start server again.

like image 160
Kamlesh Kanazariya Avatar answered Dec 28 '22 08:12

Kamlesh Kanazariya