4

I have gone through the introduction to jshell guide and couldn't find description/examples about the -C option in jshell.

$jshell --help
 -C<flag>              Pass <flag> to the compiler.
                        Use one -C for each compiler flag or flag argument
Rahul Sharma
  • 5,614
  • 10
  • 57
  • 91

2 Answers2

5

You can use this option to make use of javac command line arguments for example consider this piece of code:

String str = (String)"Hello"

Starting jshell normally and using the same would result as:

enter image description here


While at the same time you can enable compiler errors on warning(-Werror) and make use of the -Xlint key cast while compiling to warn you of the explicit cast used in the above code using -

jshell -C-Xlint:cast -C-Werror

and the same statement would result into warnings and error from Jshell compiler as:-

enter image description here


Though IMO, this certainly is documented far less in terms of what all flags shall/shall not be used while using the JShell -C command line option.

Naman
  • 27,789
  • 26
  • 218
  • 353
1

To pass options to the javac compiler that jshell is going to run to compile the statements in your REPL. One simple example (to see which classes are loaded), jshell -C-verbose. See also Standard Options for javac and Extra Options for javac for additional options you might specify.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Not very sure of all the javac options. I tried `jshell -C-version` for instance and it resulted into `java.lang.IllegalArgumentException: invalid flag: -version` – Naman Oct 14 '17 at 06:29
  • 1
    @nullpointer True, of course `-version` prints the version and exits; so supporting it wouldn't make much sense. I was simply trying to answer as to the purpose of the `-C` option. – Elliott Frisch Oct 14 '17 at 06:36
  • Ya, even I thought it's worth mentioning the same. And there is no clear documentation around what all flags categorize themselves to be used by Jshell as well. – Naman Oct 14 '17 at 06:38
  • 1
    Since these arguments are being passed to the compiler that is used to compile snippets, inappropriate options can cause JShell to fail. – Robert Field Oct 21 '17 at 20:53
  • @RobertField I assume its the same case with [J and R Flags](https://stackoverflow.com/questions/46399679/what-is-the-exact-meaning-purpose-of-the-j-and-r-flags-in-jshell?noredirect=1&lq=1) as well? – Naman Oct 21 '17 at 21:03