Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joewalnes
Created November 20, 2017 18:58
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joewalnes/4bf3ac8abc143225fe2c75592d314840 to your computer and use it in GitHub Desktop.
Save joewalnes/4bf3ac8abc143225fe2c75592d314840 to your computer and use it in GitHub Desktop.
Minimal embedded HTTP server in Kotlin using Java built in HttpServer
import com.sun.net.httpserver.HttpServer
import java.io.PrintWriter
import java.net.InetSocketAddress
/**
* Minimal embedded HTTP server in Kotlin using Java built in HttpServer
*/
fun main(args: Array<String>) {
HttpServer.create(InetSocketAddress(8080), 0).apply {
createContext("/hello") { http ->
http.responseHeaders.add("Content-type", "text/plain")
http.sendResponseHeaders(200, 0)
PrintWriter(http.responseBody).use { out ->
out.println("Hello ${http.remoteAddress.hostName}!")
}
}
start()
}
}
@IgorLo
Copy link

IgorLo commented Mar 22, 2020

🤣🤣🤣🤣🤣🤣🤣🤣

@devaughnsaxa
Copy link

It is not recommended to use the package com.sun.* if you plan to move to other JDK like OpenJDK.
It's a proprietary package owned by Oracle/Sun.
Read more at https://www.oracle.com/java/technologies/faq-sun-packages.html

@nazzinoz
Copy link

nazzinoz commented May 6, 2023

It is not recommended to use the package com.sun.* if you plan to move to other JDK like OpenJDK. It's a proprietary package owned by Oracle/Sun. Read more at https://www.oracle.com/java/technologies/faq-sun-packages.html

Don't be confused. "com.sun" and "sun" are 2 different packages. Some of the "com.sun" API are internal, but HttpServer is not. It's a part of standard public APIs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment