Pedro
Pedro

Reputation: 383

Unable to configure IntelliJ to use Java 11

Although IntelliJ IDE doesn't give me any warning, this two-line method causes a compilation error Error:(12, 37) java: incompatible types: byte[] cannot be converted to int:

import java.math.BigInteger;

public class Test {

    public static void main(String[] args){
        String foo = "123";
        new BigInteger(foo.getBytes(), 1, 1);
    }
}

In JDK 11 there is a valid constructor with this signature public BigInteger(byte[] val, int off, int len)(line 306 of java.math.BigInteger) but not in JDK 8. I was able to compile it directly with javac for JDK 11 but it fails with JDK 8 as expected. I have configured IntelliJ to use JDK 11 but I don't know why it's using an older version... System.out.println(System.getProperty("java.version")) prints 11.0.1. Seems like it's compiling with JDK 8 and running it with JDK 11.

I have checked I have JDK 11 selected in the following places:

I am using IntelliJ IDEA 2018.2.6 (Community Edition). Problem persists after restarting IntelliJ.

Edit:

project uses maven

Upvotes: 1

Views: 2002

Answers (1)

itwasntme
itwasntme

Reputation: 1462

There can be few factors of similar problems, mainly caused by other tools being in use (e.g. maven or gradle).

When using just the IDE to compile and run code there should be no problem (as the used Java version was fully configured).

The build tools (maven,gradle) can be set to use different version of Java then the one configured in IDE.

To see the version use by those tools run in terminal:

gradle -version
mvn -version

The output should contain of Java version configured with those tools.
Their Java version mostly uses the JAVA_HOME environment variable, which points to the JRE/JDK location.

If the build tools are using the different Java version to compile the sources, then the version which starts the application may lead to such errors and exception.
That problems are related to the changes of standard API between Java8 and newer versions.

Changing the value of JAVA_HOME variable to point to the Java11 folder should do the work, but if the older version is still needed then, for example when using Maven, it's possible to use the plugin:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.11</source>
        <target>1.11</target>
    </configuration>
</plugin>

Upvotes: 2

Related Questions