Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 9 no class definition exception

So i want to try the http client

package com.company;

import jdk.incubator.http.HttpClient;

public class Main {

public static void main(String[] args) {
    HttpClient client =  HttpClient.newHttpClient();

  }
}

And my module info looks like this

module com.company {
    requires jdk.incubator.httpclient;
}

But i get java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient

And I don't really understand why. My java version is "build 9-ea+ 169" and I use the latest version of IntelliJ idea (2017.1.3). I looked into this answer and it looks like I have to just add requirement into a file, but it doesn't work for some reason.

like image 448
EmberTraveller Avatar asked May 18 '17 07:05

EmberTraveller


People also ask

How do I fix No class Def Found error in Java?

NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.

What is difference between Classnotfound and Noclassdefined exception?

ClassNotFoundException is an exception that occurs when you try to load a class at run time using Class. forName() or loadClass() methods and mentioned classes are not found in the classpath. NoClassDefFoundError is an error that occurs when a particular class is present at compile time, but was missing at run time.

Why no class def found error?

The NoClassDefFoundError is a runtime error in Java that occurs if the Java Virtual Machine (JVM) or a ClassLoader instance attempts to load the definition of a class that could not be found. The class definition exists at compile-time but is not available at runtime.


1 Answers

works fine for me if I use --add-modules jdk.incubator.httpclient as the start-up parameter.

HttpClient client = HttpClient.newHttpClient();
client.executor().execute(() -> System.out.println("Here")); // prints Here

If you say that your module requires it, does not mean it will be included; at it is not included by default.

like image 197
Eugene Avatar answered Nov 13 '22 19:11

Eugene