How to add options when compiling Java

689 views
Skip to first unread message

Yue Zhao

unread,
Oct 21, 2021, 4:27:14 AM10/21/21
to bazel-discuss
I hope to use jdk11 to compile the source code of kythe through bazel, but some packages in the source code are not supported by default in jdk11. I need to add options such as "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" to compile. How can I set it in BUILD or bazelrc file to achieve this effect?

Rupert Shuttleworth

unread,
Oct 26, 2021, 7:09:46 AM10/26/21
to Yue Zhao, bazel-discuss
Have you tried javacopts?

https://docs.bazel.build/versions/main/be/java.html#java_library

On Thu, Oct 21, 2021 at 7:27 PM Yue Zhao <zyn...@gmail.com> wrote:
>
> I hope to use jdk11 to compile the source code of kythe through bazel, but some packages in the source code are not supported by default in jdk11. I need to add options such as "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" to compile. How can I set it in BUILD or bazelrc file to achieve this effect?
>
> --
> You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/3434b67d-fe04-4df0-ad0f-4dbf6dbe2ce8n%40googlegroups.com.

Olivier Notteghem

unread,
Oct 26, 2021, 4:20:14 PM10/26/21
to Rupert Shuttleworth, Yue Zhao, bazel-discuss
I think what you want is ‘jvm_opts’ from java_toolchain() - https://docs.bazel.build/versions/main/be/java.html#java_toolchain

Something like :
java_toolchain(
...
jvm_opts = ["--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED”]


Yue Zhao

unread,
Oct 26, 2021, 11:17:48 PM10/26/21
to bazel-discuss
Yes, I found this configuration parameter, but now I have a new problem.
Now I set default_java_toolchain added the parameters I need. In BUILD.
load(
  "@bazel_tools//tools/jdk:default_java_toolchain.bzl",
  "default_java_toolchain", "DEFAULT_TOOLCHAIN_CONFIGURATION", "JDK9_JVM_OPTS", "DEFAULT_JAVACOPTS"
)
default_java_toolchain(
  name = "bootstrap_toolchain",
  #configuration = DEFAULT_TOOLCHAIN_CONFIGURATION,      # One of predefined configurations
                                                        # Other parameters are from java_toolchain rule:
  java_runtime = "@bazel_tools//tools/jdk:remote_jdk11",            # JDK to use for compilation and toolchain's tools execution
  jvm_opts = JDK9_JVM_OPTS + [
                "--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
                "--add-modules=java.base,java.compiler",
                "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
            ],      # Additional JDK options
  javacopts = DEFAULT_JAVACOPTS, # Additional javac options
)

And it is set in the .bazelrc file

build --java_language_version=11
build --java_toolchain=@bazel_tools//tools/jdk:toolchain 
build --javabase=@bazel_tools//tools/jdk:remote_jdk11 
build --host_java_toolchain=@bazel_tools//tools/jdk:toolchain 
build --host_javabase=@bazel_tools//tools/jdk:remote_jdk11 

build:remote --host_javabase=@bazel_tools//tools/jdk:remote_jdk11 
build:remote --javabase=@bazel_tools//tools/jdk:remote_jdk11 
build:remote --host_java_toolchain=@bazel_tools//tools/jdk:toolchain 
build:remote --java_toolchain=@bazel_tools//tools/jdk:toolchain 


Now the compilation is successful, but it seems that all the java files still rely on jdk8 to compile after the compilation results are decompiled by javap - v
javap.png
I don't understand why jdk11 is not used. Am I not configured correctly?

Olivier Notteghem

unread,
Oct 27, 2021, 12:49:46 PM10/27/21
to Yue Zhao, bazel-discuss
<javap.png>
I don't understand why jdk11 is not used. Am I not configured correctly?
在2021年10月27日星期三 UTC+8 上午4:20:14<oliv...@uber.com> 写道:
I think what you want is ‘jvm_opts’ from java_toolchain() - https://docs.bazel.build/versions/main/be/java.html#java_toolchain

Something like :
java_toolchain(
...
jvm_opts = ["--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED”]


On Oct 26, 2021, at 4:09 AM, 'Rupert Shuttleworth' via bazel-discuss <bazel-...@googlegroups.com> wrote:

Have you tried javacopts?

https://docs.bazel.build/versions/main/be/java.html#java_library

On Thu, Oct 21, 2021 at 7:27 PM Yue Zhao <zyn...@gmail.com> wrote:

I hope to use jdk11 to compile the source code of kythe through bazel, but some packages in the source code are not supported by default in jdk11. I need to add options such as "--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED" to compile. How can I set it in BUILD or bazelrc file to achieve this effect?

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/3434b67d-fe04-4df0-ad0f-4dbf6dbe2ce8n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

Yue Zhao

unread,
Oct 27, 2021, 11:09:32 PM10/27/21
to bazel-discuss
I added such a configuration and did not modify other configurations.
config.png

Unfortunately, the compilation results have not changed. From the command javap -v, java files still rely on jdk8 to compile
Reply all
Reply to author
Forward
0 new messages