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

Gunnar Morling gunnar at hibernate.org
Tue Dec 18 09:04:08 UTC 2018


Thanks for the confirmation!

> but maybe we could add a Note with the currently applicable options.

Yes, that'd be helpful.

> 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.

I see. I feel this convolutes one prime use case of source file mode
though, which is using it to explore new (including preview) language
features. Given I'm explicitly opting into using the preview mode, it
IMO adds unnecessary noise to the program output. E.g. see this little
example where the node adds quite some verbosity to the output:

    https://twitter.com/gunnarmorling/status/1074635092566384641

In addition one cannot even do what the note says, as the
-Xlint:preview option is unsupported in source file mode, too.

So if there's no way to pass -XDsuppressNotes, could you perhaps
consider to implicitly suppress that note when running in source file
mode with preview enabled?

Thanks,

--Gunnar

Am Mo., 17. Dez. 2018 um 20:04 Uhr schrieb Jonathan Gibbons
<jonathan.gibbons at oracle.com>:
>
>
> 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