3

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.

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?

4
  • 3
    Don't treat com.sun.xxx packages as if they're part of the standard library. Oracles java distribution may the most common, but it isn't the only one. Commented May 9, 2015 at 0:05
  • 1
    What is the solution, in that case? I'm not biased on the answer, that's why I am asking. Commented May 9, 2015 at 0:07
  • Did it really say that the package did not exist? What was the actual error message? Literally, not a paraphrase. Precision is important. Commented May 10, 2015 at 0:08
  • It did really say it did not exist, but it was something very simple after all. See my answer. At least I was genuinely tricked by the package name, thinking it was actually the class I wanted. Of course that was largely because I am new to Java, but it would be nice to help other newbies who get stuck on the same thing. Commented May 10, 2015 at 11:36

2 Answers 2

6

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Secondly, why not use libraries from apache ? See 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.

3 Comments

The sun.* classes are internals of the implementation, and there is a specific warning in the Javadoc saying so and cautioning against their use in application code. com.sun.* classes are supported, as in this case, and as in the case of JNDI providers for example.
Even the com.sun classes are proprietary classes of Oracle, and not contained in other Non-Oracle JDK/JRE. This should be reason enough to avoid using those classes.
@ChristophBimminger Eclipse Temurin is a non-Oracle JDK to my knowledge and still it contains com.sun classes like in particular com.sun.net.httpserver.* . At least up to version JDK17.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.