Search code examples
javaintellij-ideahttpserver

Unable to use any java HttpServer methods


I have a really simple java HttpServer class. But for some reason the HttpServer methods, such as create(), createContext()are unusable and marked by Intellij as ''Cannot resolve symbol [methodname]''.

package httpserver;
import javax.net.ssl.HttpsURLConnection;
import java.net.InetSocketAddress;

public class HttpServer {

public static void main(String args[]) throws Exception {

    int port = 9000;
    HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
    System.out.println("Server started at " + port);
    server.createContext("/", new RootHandler());
}
}

Maven dependency I am using for this:

   <dependency>
        <groupId>com.sun.net.httpserver</groupId>
        <artifactId>http</artifactId>
        <version>20070405</version>
        <scope>test</scope>
    </dependency>

I already tried to an invalidate caches restart and rebuild but nothing seems to work. I can't find a solution to this.

This is what I am trying to use: https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/HttpServer.html


Solution

  • You named your own class HttpServer and you didn't import the other one or fully-qualify its access.

    So the compiler is trying to find a create method in your class.

    Even though it can be fixed in a different way, it's probably a good idea to start by renaming your own class to avoid that kind of confusion.

    Then make sure to import the actual class you want to reference.