After opening an existing Netbeans 8 project in Apache Netbeans 10, and setting the Java version to the newest JDK 11, Netbeans is still unable to resolve references to the new java.net.http
package which includes improved HTTP handling with classes such as HttpClient
, HttpRequest
, and HttpResponse
.
What needs to be done to make the new java.net.http
package visible to the existing project in Apache Netbeans 10?
In order to make the new java.net.http
package visible to your project, you'll need to configure your project so that it includes the module name "java.net.http" (found at the top of the Javadoc page for the package).
The existing Java project imported from Netbeans 8 will not have any knowledge of the module system introduced in Java 9, so initially you'll have no way to add a module requirement. To fix this, right-click on your Java project in Apache Netbeans 10 and then select "New" and then "Java Module Info...". In the dialog which appears, check the details and click the "Next" button and then confirm that you're happy to move entries out of the classpath and into the modulepath if offered. You'll now find a new file "module-info.java" in the default package of your project (under "Source Packages"/"<default package>").
Open the "module-info.java" file and then check your project for error markers (the angry red circles on the file icon, showing that the file contains a parsing or compilation error). Open the files which report errors and you'll probably find that some of the import statements at the top of your Java files now report an error such as this:
"Package javax.xml.stream is not visible:
(package javax.xml.stream is declared in module java.xml but module MyApplication does not read it)"
This error would mean that you'd need to add the following line to the module MyApplication
definition (where "MyApplication" will be a name based on your own project) found within your "module-info.java" file:
requires java.xml;
Save that change and you should now see the specific error about javax.xml.stream
disappear. Repeat this process until all of the visibility errors vanish from your project. (If your project doesn't use any non-core modules then you may not see any errors at all.)
Finally, once all other visibility errors are out of the way, add this line to your module MyApplication
definition:
requires java.net.http;
Save that change, and now when editing your project code in Apache Netbeans IDE 10 you should be able to see and use the new java.net.http
classes such as HttpClient
.