1

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:

  • Run/Debug Configurations > JRE
  • Project Structure > Project > Project SDK
  • Project Structure > Project > Project Language Level
  • Project Structure > Modules > Language Level

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

Edit:

project uses maven

Pedro
  • 355
  • 4
  • 18
  • In my original code, I have a number encoded in base 64 and I get the same issue, I just simplified the problem. – Pedro Dec 17 '19 at 22:39
  • 3
    Your code doesn't make sense. If you want to convert a String to a BigInteger, use `new BigInteger(String)`. –  Dec 17 '19 at 22:39
  • [Decode Base64 in Java](https://stackoverflow.com/questions/469695/decode-base64-data-in-java) and [many more answers](https://stackoverflow.com/search?q=[java]+base64+decode) –  Dec 17 '19 at 22:41
  • 2
    Looking at the javadoc: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigInteger.html the constructor that accepts a `byte[]` should be a 'byte sub-array containing the two's-complement binary representation of a BigInteger.' Not a string encoded byte array. – KyleAure Dec 17 '19 at 22:41
  • @a_horse_with_no_name yes I using that in my original code and had the same issue – Pedro Dec 17 '19 at 22:43
  • 2
    Works for me with JDK 11. – ldz Dec 17 '19 at 22:43
  • @KyleAure what does "two's-complement binary representation of a BigInteger" mean? Shouldn't that be a problem after compilation, I am passing a byte[] anyways – Pedro Dec 17 '19 at 22:52
  • This seems like an IntelliJ issue, I was able to compile it through command line – Pedro Dec 17 '19 at 23:12
  • @Pedro correct, at compile time you should not have any issues with the code as it is written here. – KyleAure Dec 17 '19 at 23:14
  • @ArvindKumarAvinash yes, it should work, it seems like an IntelliJ issue... – Pedro Dec 17 '19 at 23:18
  • @ArvindKumarAvinash done – Pedro Dec 17 '19 at 23:39
  • @Pedro what you get if you print java version with `System.out.println(System.getProperty("java.version"))` ? – itwasntme Dec 17 '19 at 23:43
  • Prints 11.0.1, very confusing. Is it compiling with JDK 8 and running it with JDK 11? – Pedro Dec 17 '19 at 23:45
  • Are you using gradle or maven or something similar? (And I assume your run configuration in IntelliJ is standard application) – itwasntme Dec 17 '19 at 23:50
  • I am using maven, I had a `1.8` configuration there which I had removed earlier and the problem persists. Yes I am not doing nothing weird, not sure what you mean by 'standard application' – Pedro Dec 17 '19 at 23:53
  • Have you tried cleaning and rebuilding the project? Also what java version u get when `mvn -version`? (Additionaly try adding the – itwasntme Dec 17 '19 at 23:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204454/discussion-between-pedro-and-itwasntme). – Pedro Dec 18 '19 at 00:02

1 Answers1

2

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>
itwasntme
  • 1,442
  • 4
  • 21
  • 28