在 Eclipse Oxygen 中尝试在 java 中使用 jdk.incubator.http.HttpClient 时出现 NoClassDefFoundError
NoClassDefFoundError while trying to use jdk.incubator.http.HttpClient in java in Eclipse Oxygen
这是我使用的代码片段:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create("https://www.google.com")).GET().build();
HttpResponse.BodyHandler responseBodyHandler = HttpResponse.BodyHandler.asString();
HttpResponse response = client.send(request, responseBodyHandler);
System.out.println("Status code = " + response.statusCode());
String body = response.body().toString();
System.out.println(body);
当我 运行 上述代码时,Eclipse 为 HttpClient 抛出 NoClassDefFoundError
。但是,当我将 jshell 与 --add-modules=jdk.incubator.httpclient
一起使用时,此功能完美无缺。怎样才能让代码通过 Eclipse 执行?
感谢@Steephen who helped me with a hint in the question comments. After viewing the answers ,我尝试在示例项目的 运行 配置中添加以下内容。
之后,代码运行顺利没有抛出NoClassDefFoundError。
这是我使用的代码片段:
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder(URI.create("https://www.google.com")).GET().build();
HttpResponse.BodyHandler responseBodyHandler = HttpResponse.BodyHandler.asString();
HttpResponse response = client.send(request, responseBodyHandler);
System.out.println("Status code = " + response.statusCode());
String body = response.body().toString();
System.out.println(body);
当我 运行 上述代码时,Eclipse 为 HttpClient 抛出 NoClassDefFoundError
。但是,当我将 jshell 与 --add-modules=jdk.incubator.httpclient
一起使用时,此功能完美无缺。怎样才能让代码通过 Eclipse 执行?
感谢@Steephen who helped me with a hint in the question comments. After viewing the answers
之后,代码运行顺利没有抛出NoClassDefFoundError。