16

I'm trying to get one of my projects ready for Java 11 but for some reason Intellij can't find java.net.http. It isn't underlining it as not found in module-info.java like it would if I typed it wrong but when I try build the project I get the error below. I've tried reinstalling Intellij 2018.2.3 and uninstalling all other versions of Java. Any advice on how to get this working would be appreciated.

Error:

Information:java: Errors occurred while compiling module 'crawler'
Information:javac 11 was used to compile java sources
Information:15/09/2018 11:16 - Compilation completed with 1 error and 0 warnings in 636 ms
C:\Users\Will\IdeaProjects\crawler\src\module-info.java
Error:(2, 22) java: module not found: java.net.http

module-info.java:

module crawler {
    requires java.net.http;
}

Request.java:

package Request;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Request {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("starting download");
        String body = HttpClient.newBuilder().build().send(HttpRequest.newBuilder().uri(URI.create("https://example.com")).build(), HttpResponse.BodyHandlers.ofString()).body();
        System.out.println("finished download:" + body);
    }
}

Structure:

crawler
    src
        Request
            Request.java
        module-info.java
Will
  • 604
  • 1
  • 5
  • 18
  • 3
    Can't reproduce, please share the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – CrazyCoder Sep 15 '18 at 02:05
  • @CrazyCoder added – Will Sep 15 '18 at 02:32
  • @Will there is no `main/java` inside your src folder? – Naman Sep 15 '18 at 02:47
  • 1
    @Will [Builds and runs without issues](https://i.imgur.com/PL1nRdo.png) in IntelliJ IDEA 2018.2.3 for me. What Java 11 build do you use? What are module/project language level settings? – CrazyCoder Sep 15 '18 at 02:47
  • 1
    This comment is not apropos to your problem, but your chosen package name is wrong: 1) the name should be all lowercase, 2) the name should follow the domain name convention; see https://google.github.io/styleguide/javaguide.html#s5.2-specific-identifier-names – Stephen C Sep 15 '18 at 03:05
  • @CrazyCoder I just installed Java 11 build 26 and IntelliJ IDEA 2018.2.3 in a clean VirtualBox instance of Windows 10 and it builds for me. Could it in some way be a Windows issue since I had Java 8-11 installed until today? I'm using Java 11 build 26 and project language level 9. – Will Sep 15 '18 at 05:51
  • Ahhhhh I see. I'm dumb. The project language level obviously needs to be 11... Sorry for wasting your time! – Will Sep 15 '18 at 05:58

5 Answers5

35

In the case that the above proposed resolution (by @Will) does not solve your issue as was the case with me (i.e. setting the project language level), check to to see what the bytecode target version of your java compiler has been set to, in your project preferences: Set project preferences byte code version for java in IntelliJ

Dark Star1
  • 6,986
  • 16
  • 73
  • 121
17

I had the wrong project language level set. To use java.net.http you need it to be at least 11. To change the project language level see: https://www.jetbrains.com/help/idea/project-page.html

Hopefully this helps someone else out.

Will
  • 604
  • 1
  • 5
  • 18
16

I had the same problem with the package jdk.jfr. This is how I fixed it. It should work for you too.

In order to make it work I had to make 2 changes:

First I had to set the language level to 11; see in the picture below.

enter image description here

Then I had to adjust the Java Compiler. The Target bytecode version is 11 and I set the project bytecode version Same as language level. Then you don't have to change all of them constantly. Please see picture below.

enter image description here

Matécsa Andrea
  • 532
  • 1
  • 6
  • 15
1

For those who are having this problem in 2022, even if the solutions mentioned here did not help, I was able to figure out what the problem was and how to fix this.

First of all I wanted to make sure that the problem is not from my Maven config, so I ran the following in my terminal:

mvn package

followed by:

java -cp target/covid-cases-cli-1.0-SNAPSHOT.jar org.matrixeternal.covidcasescli.App

and it was build without any errors whatsoever. So it means something is up with IntelliJ.

I am using Java 17 and building with Maven using IntelliJ. IntelliJ uses its own internal command to build the project. To override this behaviour, you must go to Preferences - Build, Execution & Deployment - Build Tools - Maven - Runner and select the option Delegate IDE build/run actions to maven which will essentially run directly from the Maven config file using the mvn tool installed in the system, instead of using the IDE command.

Delegate IDE build/run actions to maven option

r.gjoni
  • 45
  • 7
0

Set the compiler for IntelliJ as Java 11 IntelliJ Idea-> Preferences-> Build, Execution, Deployment -> Java Compiler Select java 11 from the drop down

apurva
  • 1