client_java icon indicating copy to clipboard operation
client_java copied to clipboard

References to internal com.sun.* packages in simpleclient_httpserver

Open albsch opened this issue 4 years ago • 11 comments

The HTTPServer.java class (in simpleclient_httpserver) references internal com.sun.* packages, which makes the server unsable for modern Java build setups:

import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpExchange;

Starting the server results in the exception:

java.lang.NoClassDefFoundError: com/sun/net/httpserver/HttpHandler

Tested with simpleclient_httpserver v0.8.1 and openjdk version "12.0.2" 2019-07-16 on a Jigsaw Java project.

Similar to issue #478.

albsch avatar Feb 26 '20 10:02 albsch

How do you propose keeping this working while still supporting Java 6?

brian-brazil avatar Feb 26 '20 10:02 brian-brazil

I don't know. Is support for newer Java releases planned while providing support for Java 6? I'm interested to know if I can use this dependency in the near future with up to date Java versions.

albsch avatar Feb 26 '20 10:02 albsch

I hope to support newer versions also, however Java has been going out of its way to make breaking changes.

brian-brazil avatar Feb 26 '20 10:02 brian-brazil

Java 6 is not supported since 2013 :O

llech avatar Sep 25 '20 14:09 llech

Same here. Works in some places but this is pretty broken.

shea-hawkins avatar Oct 26 '20 21:10 shea-hawkins

@brian-brazil The problem is that you're assuming com.sun is part of the JDK running the code, which isn't guaranteed. To fix this, it can be included as a transitive dependency.

Mostly:

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.

https://stackoverflow.com/questions/30134507/cannot-use-com-sun-net-httpserver-java-8

You can also switch to the apache http core, which is open-source and won't have different behaviors depending on the distribution of JDK you use (Oracle vs AdoptOpen vs Open) https://hc.apache.org/

shea-hawkins avatar Oct 26 '20 21:10 shea-hawkins

To fix this, it can be included as a transitive dependency.

How does one do this?

You can also switch to the apache http core

3rd party dependencies are purposefully avoided in the library where we don't strictly need them, to avoid potential conflicts with dependencies an application itself may have.

brian-brazil avatar Oct 26 '20 22:10 brian-brazil

@brian-brazil this may be a case where you strictly need them. The only dependency on central that I can find for this library is this unofficial one: https://mvnrepository.com/artifact/com.sun.net.httpserver/http/20070405

shea-hawkins avatar Oct 26 '20 22:10 shea-hawkins

Are you saying that the standard Java 8 JREs come with no builtin HTTP server? That doesn't sound right at all.

brian-brazil avatar Oct 26 '20 23:10 brian-brazil

sun.* packages are Oracle-specific and may not exist on non-Oracle JDKs/JREs. Code that calls sun.* packages is technically not 100% Java compliant.

Reference: https://www.oracle.com/java/technologies/faq-sun-packages.html

dhoard avatar Sep 14 '21 22:09 dhoard

Getting the same error when trying to use it on Android.

ylz-at avatar Sep 22 '21 05:09 ylz-at