Java 9 has a read-eval-print loop for Java, called jshell
. I've seen it work in it's basic mode, from the command line. Can it also be used in a remote process? In other words, can I connect to another Java process and enter code snippets to run within that runtime? This would be a nice way to change configuration state in an app server without having to write an admin tool with a UI.
The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. The tool is run from the command line.
JShell is a Java read-eval-print loop which was first introduced in the JDK 9.
To load a file into the JShell tool, we can use the "/open" command. The "/open" command has loaded the "Test. java" file into a session. The "/vars" command can be used to load the variables into a session and "/methods" command can be used to load the methods into a session.
What is JShell? One of the new features in Java 9 is JShell which is a Read-Eval-Print-Loop (REPL) interpreter. A REPL is an interactive shell that allows execution of single statements without the need to compile a complete program.
Attaching JShell is a project that implements an extension to JShell for exactly this. It uses the Java Agent for communication with the target JVM.
I have not used it so I cannot say how well it works.
Start the target JVM with -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=XXXhostname:XXXport
(update XXXhostname
and XXXport
as appropriate) and call new uk.org.cinquin.attaching_jshell.ExistingVMRemoteExecutionControl()
from that JVM prior to using JShell.
call JShell as follows: java -cp lib/attaching_jshell.jar jdk.internal.jshell.tool.JShellToolProvider --execution "attachToExistingVM:hostname(XXXhostname),port(XXXport)"
using the same values of XXXhostname
and XXXport
as above
Run code in the remote JVM like this:
import uk.org.cinquin.attaching_jshell.ExistingVMRemoteExecutionControl;
String s = ExistingVMRemoteExecutionControl.theGoodsForTesting
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With