不能使用com.sun.net.httpserver - Java 8

时间:2023-01-22 09:05:16

I am trying to create a simple server program to handle http requests. So with minimum search, I stumbled upon the oracle documentation for the httpserver class, inside the com.sun.net package. I'm relatively new to Java, so I thought that a class "sponsored" by Oracle itself would be included in the default libraries.

我正在尝试创建一个简单的服务器程序来处理http请求。因此,通过最少的搜索,我偶然发现com.sun.net包中的httpserver类的oracle文档。我对Java比较陌生,所以我认为Oracle本身“赞助”的类将包含在默认库中。

Unfortunately, that was not the case. After a lot of trials for possible syntax-import errors (various kinds of error arouse) and having installed the Oracle JDK 8 correctly on my Ubuntu machine, the compiler said that the package did not exist. What do I have to do to get the package to work?

不幸的是,事实并非如此。经过大量试验可能的语法导入错误(各种错误引起)并在我的Ubuntu机器上正确安装了Oracle JDK 8之后,编译器说该包不存在。我需要做些什么才能使包运行起来?

2 个解决方案

#1


Firstly try to avoid com.sun.xxx package, as those are internalls of Oracle/Sun java implementation.

首先尝试避免使用com.sun.xxx包,因为它们是Oracle / Sun java实现的内部。

Secondly, why not use libraries from apache ? See https://hc.apache.org/

其次,为什么不使用apache库?见https://hc.apache.org/

EDITED:

You can also look on http://sparkjava.com/ , not tested but examples looks promising and they are using Java 8 nice features.

您也可以查看http://sparkjava.com/,未经测试,但示例看起来很有前途,他们正在使用Java 8的不错功能。

#2


I did finally make it work. Mostly, it was a misunderstanding from my place, since I only imported up to a point that was wrong - that is,I only imported com.sun.net.httpserver, thinking the latter part was the actual class I wanted, but it was merely the package name. So then I proceeded to import com.sun.net.httpserver.HttpServer, then the rest of my classes. Finally a com.sun.net.httpserver.* would work perfectly fine. It seems stupid now that I figured it out, but I think I will leave it here just in case anyone has the same misunderstanding - I already see 1 favourite on the question. And of course, as others have pointed out, the package is not part of the standard java libraries, but I used Oracle Java specifically for that.

我终于做到了。大多数情况下,这是一个误解我的地方,因为我只输入了一个错误的点 - 也就是说,我只导入了com.sun.net.httpserver,认为后一部分是我想要的实际课程,但它是只是包名。然后我继续导入com.sun.net.httpserver.HttpServer,然后导入其余的类。最后一个com.sun.net.httpserver。*可以正常工作。现在看来我觉得很愚蠢,但我想我会留在这里以防万一有人有同样的误解 - 我已经在问题上看到了一个最喜欢的东西。当然,正如其他人所指出的那样,软件包不是标准java库的一部分,但我专门为此使用了Oracle Java。

P.S. The class is really useful, unlike what the other answer implies, but now I have stumbled upon another problem regarding reading the request body right, something that might have to do with the locale of the client-server, and I will now procced to search that.. Just a warning for anyone thinking of using the package.

附:该类非常有用,与其他答案所暗示的不同,但现在我偶然发现了另一个关于读取请求正文的问题,这可能与客户端 - 服务器的语言环境有关,我现在将开始搜索那个..只是警告任何想要使用这个包的人。

#1


Firstly try to avoid com.sun.xxx package, as those are internalls of Oracle/Sun java implementation.

首先尝试避免使用com.sun.xxx包,因为它们是Oracle / Sun java实现的内部。

Secondly, why not use libraries from apache ? See https://hc.apache.org/

其次,为什么不使用apache库?见https://hc.apache.org/

EDITED:

You can also look on http://sparkjava.com/ , not tested but examples looks promising and they are using Java 8 nice features.

您也可以查看http://sparkjava.com/,未经测试,但示例看起来很有前途,他们正在使用Java 8的不错功能。

#2


I did finally make it work. Mostly, it was a misunderstanding from my place, since I only imported up to a point that was wrong - that is,I only imported com.sun.net.httpserver, thinking the latter part was the actual class I wanted, but it was merely the package name. So then I proceeded to import com.sun.net.httpserver.HttpServer, then the rest of my classes. Finally a com.sun.net.httpserver.* would work perfectly fine. It seems stupid now that I figured it out, but I think I will leave it here just in case anyone has the same misunderstanding - I already see 1 favourite on the question. And of course, as others have pointed out, the package is not part of the standard java libraries, but I used Oracle Java specifically for that.

我终于做到了。大多数情况下,这是一个误解我的地方,因为我只输入了一个错误的点 - 也就是说,我只导入了com.sun.net.httpserver,认为后一部分是我想要的实际课程,但它是只是包名。然后我继续导入com.sun.net.httpserver.HttpServer,然后导入其余的类。最后一个com.sun.net.httpserver。*可以正常工作。现在看来我觉得很愚蠢,但我想我会留在这里以防万一有人有同样的误解 - 我已经在问题上看到了一个最喜欢的东西。当然,正如其他人所指出的那样,软件包不是标准java库的一部分,但我专门为此使用了Oracle Java。

P.S. The class is really useful, unlike what the other answer implies, but now I have stumbled upon another problem regarding reading the request body right, something that might have to do with the locale of the client-server, and I will now procced to search that.. Just a warning for anyone thinking of using the package.

附:该类非常有用,与其他答案所暗示的不同,但现在我偶然发现了另一个关于读取请求正文的问题,这可能与客户端 - 服务器的语言环境有关,我现在将开始搜索那个..只是警告任何想要使用这个包的人。