Hello,
I try to create a kotlin script http.kts
:
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
val client = HttpClient.newBuilder().build()
val request = HttpRequest.newBuilder()
.uri(URI.create("https://google.com/"))
.build()
val response = client.send(request, HttpResponse.BodyHandlers.ofString());
println(response.body())
kotlinc -script http.kts
java.lang.NoClassDefFoundError: java/net/http/HttpClient
at Http.(http.kts:7)
kotlinc -version
info: kotlinc-jvm 1.3.72 (JRE 11.0.7+8-LTS)
Any idea ?
It’s work with Java 11 JEP 330 : Launch Single-File Source-Code Programs
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Http {
public static void main(String[] args) {
var client = HttpClient.newBuilder().build();
var request = HttpRequest.newBuilder()
.uri(URI.create("https://google.com/"))
.build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
} catch (Exception e) {
e.printStackTrace();
}
}
}
java Http.java
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML```