What is the exact meaning / purpose of the J and R flags in jshell?

心不动则不痛 提交于 2019-11-30 20:40:35

As I understand it, JShell has 3 main 'places' to execute code:

Using jshell tool, we have no current process before launch, so we have only two options - use one JVM (locally), or use two JVMs - one for JShell client (locally) and another for the execution engine (possibly remotely).

The interesting thing is, JShell always launch two JVMs by default, as the hard-coded --execution key is "failover:0(jdi:hostname(" + loopback + ")),1(jdi:launch(true)), 2(jdi)" (see JShell class source code).

Closer to the point. I've made a couple of experiments with -verbose option and checked JVM options in runtime with ManagementFactory.getRuntimeMXBean().getInputArguments().

  • jshell -J-verbose command

    Printed -verbose output in the console.

    No -verbose option in the input arguments: [-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:63305]

  • jshell -R-verbose command

    No -verbose output in the console.

    Printed -verbose option in the input arguments: [-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:63339, -verbose]

  • jshell --execution="local" -J-verbose command

    Printed -verbose output in the console.

    Printed -verbose option in the input arguments: [-Dapplication.home=C:\Program Files\Java\jdk-9, -Xms8m, -verbose, -Djdk.module.main=jdk.jshell]

  • jshell --execution="local" -R-verbose

    No -verbose output in the console.

    No -verbose option in the input arguments: [-Dapplication.home=C:\Program Files\Java\jdk-9, -Xms8m, -Djdk.module.main=jdk.jshell]

TL;DR

Remote execution (default case, execution over JDI)

-J<flag> passes option to the JShell client JVM

-R<flag> passes option to the execution engine JVM

Local execution (--execution="local")

-J<flag> passes option to the only present JVM

-R<flag> does nothing

I still find the scope of explaining the usage of the flags used in both the attributes for the question to be answered completely, hence putting that in words here.

-J flag is used to provide a runtime argument to the JShell which is similar to the way one provides while performing via an IDE under Run -> Configuration to specify arguments as -Dkey=value.

The usage of the attribute is documented and quite similar to ones illustrated for the flag -C just that the -J flags are instead the java command line options. For example, using the -XX:+PrintCommandLineFlags would detail out the runtime flags used by the current JVM.

So, the default values of command line flags used by your JShell instance(without setting any additional flag) could be as:-

But let's say you don't want to make use of the CompactStrings of Java9 in your JShell execution you can tell the JVM using the -J flag and -XX:-CompactStrings attribute to do so, as -

jshell -R-XX:+PrintCommandLineFlags -R-XX:-CompactStrings

would list out the following output:

➜ Similar java command line options/flags, when attached with and used to start JShell on a remote JVM, are linked to -R attribute of the JShell during remote execution.

jshell -R-XX:+PrintCommandLineFlags -R-XX:-CompactStrings

Anatoly's answer has got some good amount of research attached to it and I would suggest that to be read to understand about control and execution engine of the JShell for local Vs remote execution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!