How to pass compiler options when running a single-file source-code Java program?

Jonathan Gibbons jonathan.gibbons at oracle.com
Mon Dec 17 19:03:40 UTC 2018


On 12/16/18 2:04 AM, Gunnar Morling wrote:
> Hi,
>
> I'd like to use JEP 330 [1] to run a single-file source-code program
> with Java (>= 11).
>
> Doing so, I'd like to pass options understood by javac but not by the
> runtime (java), e.g. -XDsuppressNotes. But this causes e.g. the
> following invocation to fail:
>
>      java --enable-preview --source=12 -XDsuppressNotes Test.java
>
>      Unrecognized option: -XDsuppressNotes
>      Error: Could not create the Java Virtual Machine.
>      Error: A fatal exception has occurred. Program will exit.
>
> There's this related sentence in the JEP:
>
>> The source-launcher implementation class has access to any relevant
>> command-line options, such as those to define the class path, module path,
>> and the module graph, and passes those options to the compiler to configure
>> the compilation environment.
> So I'd expect an option such as -XDsuppressNotes to be propagated, but
> it seems there's no authoritative list of options propagated to javac
> defined.

That's not the intent of the sentence in the JEP.

The intent of the sentence is that if there are any runtime options
that could/should reasonably be applied in the context of the compilation
environment, then those options will be passed through to the
compiler.  There has never been any intent to provide a mechanism
to pass arbitrary options to the compiler when using the source-launcher
feature of the Java launcher.

The set of applicable options was not specified explicitly, so as not
to preclude options being added in future -- but maybe we could
add a Note with the currently applicable options.  The list is

--add-exports
--add-modules
--class-path -classpath -cp
--enable-preview
--limit-modules
--module-path -p
--patch-module
--upgrade-module-path


>
> Is there any way to pass this option when implicitly invoking the
> compiler through the java command in this case?

No.  If you want to specify arbitrary javac options, use javac.

I would also note that you are trying to use a -XD option.
That is a non-standard, undocumented, unsupported option for javac.
Even if we were to open up access to some additional javac options
when using the source launcher, it is not likely that we would expose
that particular option.

-- Jon

>
> Thanks,
>
> --Gunnar
>
> [1] https://openjdk.java.net/jeps/330


More information about the compiler-dev mailing list