Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazel cannot set --host_javabase to less than JDK 11 #8750

Closed
bmuschko opened this issue Jun 28, 2019 · 4 comments
Closed

Bazel cannot set --host_javabase to less than JDK 11 #8750

bmuschko opened this issue Jun 28, 2019 · 4 comments
Labels
area-EngProd Bazel CI, infrastructure, bootstrapping, release, and distribution tooling P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Rules-Java Issues for Java rules type: feature request

Comments

@bmuschko
Copy link

Description of the problem / feature request:

As a user I want to compile my program with JDK 10 instead of default JDK 11 version that comes with Bazel. Let's say the reason for it is that my production system relies on Java 10 features. In the following example I want to make sure that I can compile with JDK 10 which still includes the module java.xml.bind. The module has been removed in Java 11 and therefore fails with error: module not found: java.xml.bind.

JAVA_VERSION_OPTS = [
    "-source 10",
    "-target 10",
    "--add-modules java.xml.bind"
]

java_binary(
    name = "hello-world",
    srcs = glob(["src/main/java/com/bmuschko/**/*.java"]),
    main_class = "com.bmuschko.HelloWorld",
    javacopts = JAVA_VERSION_OPTS,
)

Bazel itself seems to set a target compatibility of Java 11 and therefore cannot run with < Java 11 anymore.

bazel build //:hello-world --host_javabase @bazel_tools//tools/jdk:remote_jdk10
INFO: Build option --host_javabase has changed, discarding analysis cache.
INFO: Analyzed target //:hello-world (0 packages loaded, 598 targets configured).
INFO: Found 1 target...
ERROR: /Users/bmuschko/dev/projects/bazel-examples/java/java-toolchain/BUILD:7:1: Building hello-world.jar (3 source files) failed: Worker process did not return a WorkResponse:

---8<---8<--- Start of log, file at /private/var/tmp/_bazel_bmuschko/4eccd64d85169c0587dd4ea563eb5b76/bazel-workers/worker-3-Javac.log ---8<---8<---
Exception in thread "main" java.lang.UnsupportedClassVersionError: javax/lang/model/element/TypeElement has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 54.0
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
	at java.base/java.lang.ClassLoader.defineClass(Unknown Source)
	at java.base/java.security.SecureClassLoader.defineClass(Unknown Source)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(Unknown Source)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassInModuleOrNull(Unknown Source)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(Unknown Source)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(Unknown Source)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(Unknown Source)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
	at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
	at com.google.devtools.build.buildjar.BazelJavaBuilder.parse(BazelJavaBuilder.java:137)
	at com.google.devtools.build.buildjar.BazelJavaBuilder.processRequest(BazelJavaBuilder.java:91)
	at com.google.devtools.build.buildjar.BazelJavaBuilder.runPersistentWorker(BazelJavaBuilder.java:68)
	at com.google.devtools.build.buildjar.BazelJavaBuilder.main(BazelJavaBuilder.java:46)
---8<---8<--- End of log ---8<---8<---
Target //:hello-world failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.527s, Critical Path: 0.29s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

I would expect to be able to set --host_javabase with less than Java 11.

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

See console output above

What operating system are you running Bazel on?

MacOSX 10.14.5

What's the output of bazel info release?

release 0.27.0

What's the output of git remote get-url origin ; git rev-parse master ; git rev-parse HEAD ?

git@github.com:bmuschko/bazel-examples.git
638882ca1bfcece3504cad3955398a98c39dcde1
638882ca1bfcece3504cad3955398a98c39dcde1

@davido
Copy link
Contributor

davido commented Jun 30, 2019

There is no such thing as Java 9 and Java 10. They are gone: [1].

  • Java 9 (non-LTS): End of Life: March 2018
  • Java 10 (non-LTS): End of Life: September 2018

[1] https://www.oracle.com/technetwork/java/java-se-support-roadmap.html

@bmuschko
Copy link
Author

You are right that Java 9 and 10 are EOL, however, that doesn't mean that organizations still use Java 9/10 or more commonly Java 8 (as it is a LTS version). As a datapoint, see "The State of Developer Ecosystem 2019". I personally know some organizations that still use Java 8. I also wanted to point out that you can still get updates for Java 8 if you are a paying customer. That's a new option introduces in the past 6 months I believe.

It's totally fine for Bazel to ship with JDK 11 but it should be compiled with an earlier target compatibility to allow projects to use a JDK < 11 if needed to detect incompatibilities early in the development lifecycle.

@ishikhman ishikhman added type: feature request untriaged area-EngProd Bazel CI, infrastructure, bootstrapping, release, and distribution tooling labels Jul 1, 2019
@philwo philwo added the team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website label Jun 15, 2020
@philwo philwo added team-Rules-Java Issues for Java rules and removed team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website labels Feb 8, 2021
@comius
Copy link
Contributor

comius commented Mar 3, 2021

Java compilation is quite complex in Bazel. The JDK's system libraries Bazel is using are coming from target JDK, which is by default locally installed JDK. So you should be able to compile your code by either having JDK10 installed in JAVA_HOME and on PATH or overriding it with --javabase.

--host_javabase specifies JDK that's used during compilation. You should be able to use either JDK10 or the default JDK11, since you're setting --source and --target.

May I also suggest to use Bazel 4.0.0, which is a LTS.

I will close this issue, because Bazel@HEAD significantly changes the mechanisms, javabase flags were deprecated. More info on https://docs.bazel.build/versions/master/bazel-and-java.html

@comius comius closed this as completed Mar 3, 2021
@comius comius added P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) and removed untriaged labels Mar 3, 2021
aherrmann added a commit to tweag/rules_nixpkgs that referenced this issue Dec 10, 2021
We use JDK 8 because the nixpkgs version 18.09 does not provide Java 11
yet and Java 9 and 10 cause problems with Bazel 4.0.0 and are no longer
supported releases. See
- bazelbuild/bazel#8750 (comment)
- bazelbuild/bazel#13065
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-EngProd Bazel CI, infrastructure, bootstrapping, release, and distribution tooling P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Rules-Java Issues for Java rules type: feature request
Projects
None yet
Development

No branches or pull requests

6 participants
@davido @bmuschko @philwo @comius @ishikhman and others