JDK_JAVAC_OPTIONS parsing changes between JDK 17 and 18

Jonathan Gibbons jonathan.gibbons at oracle.com
Wed Aug 7 19:53:16 UTC 2024


1. You can only use the `-J` option for tools like `javac` when invoking 
the tool through the "wrapper" in `bin/javac` (or `bin/javac.exe` on 
Windows.  `-J` is used to pass options to the VM used to run the tool, 
and is not supported when running `javac` through an API entry point in 
an already-running JVM.  This is because it is then "too late" to pass 
any options to that JVM.

2. If you use `-J` in conjunction with `--add-exports` or `-D` you are 
only affecting the JVM used to run `javac` and not the compilation 
environment used to compile your program. As such, generally, the only 
reason to do so is to affect the environment used to execute any 
annotation processors you may be using (if any).  In other words, use 
`-J-add-exports` if you want to affect the runtime environment used by 
`javac` to run annotation processors; use plain `--add-exports` if you 
want to affect the compilation environment used to compile your program.

If you are seeing a change in the behavior in the response to using 
`-J`, you should look to changes in how your system is invoking `javac`.

-- Jon

On 8/7/24 8:35 AM, Dimitri John Ledkov wrote:
> Hi,
>
> I've searched all forums, stackoverflow, release notes, documentation 
> and attempted to read openjdk source code. I am not sure if below 
> environment variables options are supposed to work, if they do 
> anything, and how they should be done. The behaviour change is in JDK 
> 18, but in practice I only care about how it should be done in JDK 11, 
> 17, 21.
>
> With java 17
>
> $ docker run -ti \
> --env 
> 'JDK_JAVAC_OPTIONS="-J--add-exports=java.base/sun.security.internal.spec=ALL-UNNAMED"' 
> \
> --entrypoint javac openjdk:17 --version
> javac 17.0.2
>
> $ docker run -ti \
> --env 'JDK_JAVAC_OPTIONS="-J-Djavax.net.ssl.trustStoreType=PKCS12"' \
> --entrypoint javac openjdk:17 --version
> javac 17.0.2
>
> And yet with java 18 (19, 20, 21, 22)
>
> $ docker run -ti \
> --env 
> 'JDK_JAVAC_OPTIONS="-J--add-exports=java.base/sun.security.internal.spec=ALL-UNNAMED"' 
> \
> --entrypoint javac openjdk:18 --version
> jerror: invalid flag: 
> -J--add-exports=java.base/sun.security.internal.spec=ALL-UNNAMED
> Usage: javac <options> <source files>
> use --help for a list of possible options
>
> $ docker run -ti \
> --env 'JDK_JAVAC_OPTIONS="-J-Djavax.net.ssl.trustStoreType=PKCS12"' \
> --entrypoint javac openjdk:18 --version
> error: invalid flag: -J-Djavax.net.ssl.trustStoreType=PKCS12
> Usage: javac <options> <source files>
> use --help for a list of possible options
>
> Can anybody point me at relevant documentation or source code changes?
> Or otherwise explain how to pass these options correctly?
> Should they even be set in JDK_JAVAC_OPTIONS?
> Should  one specify '--add-exports' without '-J' prefix?
> How does one specify trustStoreType - or does '-J-D' flag doesn't 
> actually do anything in JDK_JAVAC_OPTIONS?
>
> Ideally looking for environment variables to set in my Dockerfile, 
> rather than command line arguments.
>
> -- 
> Regards,
>
> Dimitri.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/discuss/attachments/20240807/f05f074d/attachment-0001.htm>


More information about the discuss mailing list