Home > OS >  Why I am getting java.lang.UnsupportedClassVersionError while trying to run bazel java target?
Why I am getting java.lang.UnsupportedClassVersionError while trying to run bazel java target?

Time:11-03

I am trying to compile and run the example java project. Instead of using the default JDK I have chosen JDK 15 by providing the setting --java_language_version="15". The complete command looks like this:

bazel run --java_language_version="15" //:ProjectRunner

I can see that it downloads Zulu JDK 15. But running the executable throws the following error:

java.lang.UnsupportedClassVersionError: com/example/ProjectRunner has been compiled by a more recent version of the Java Runtime (class file version 59.0), this version of the Java Runtime only recognizes class file versions up to 55.0

What I already figured out is that it tries to run under JDK/JRE 11 (class file versions up to 55.0) but has been compiled with JDK 15 (class file version 59.0).

What I don't understand is why bazel runs the java application with the default JDK (11).

CodePudding user response:

As you say, the --java_language_version flag only selects what JDK used to compile Java. The JRE used to execute Java may be configured with the --java_runtime_version flag. See https://bazel.build/docs/bazel-and-java#config-jvm.

  • Related