You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java HttpServer方法无法使用,IntelliJ提示无法解析符号

解决HttpServer.create()无法解析的问题

嘿,这个问题我太熟啦!你踩了一个Java新手很容易遇到的命名冲突坑~

问题根源

你自己定义的类名HttpServer,和JDK自带的com.sun.net.httpserver.HttpServer完全重名了!当你在main方法里写HttpServer.create()时,Java编译器会优先指向你自己写的这个类——但你的类里根本没有create()这些静态方法,所以IntelliJ才会提示“Cannot resolve symbol [methodname]”。

另外提一句,你代码里导入的javax.net.ssl.HttpsURLConnection完全没用,直接删掉就行。

两种解决办法

方法一:使用全限定类名调用JDK的HttpServer

直接在代码里指定JDK自带HttpServer的完整路径,让编译器明确知道你要调用的是哪个类:

package httpserver;
import java.net.InetSocketAddress;

public class HttpServer {
    public static void main(String[] args) throws Exception {
        int port = 9000;
        // 用全限定类名指定JDK的HttpServer
        com.sun.net.httpserver.HttpServer server = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(port), 0);
        System.out.println("Server started on port " + port);
    }
}

方法二:重命名你自己的类

更推荐这个办法——把你自己的类改成别的名字,比如MyHttpServer,这样就彻底避免了命名冲突,代码也更整洁:

package httpserver;
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;

// 重命名自己的类
public class MyHttpServer {
    public static void main(String[] args) throws Exception {
        int port = 9000;
        HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
        System.out.println("Server started on port " + port);
    }
}

内容的提问来源于stack exchange,提问作者Emily

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:07:56