问题
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.
回答1:
The simple answer is no, there is no way to attach jshell to a running Java process. jshell is a standalone app that runs in its own JVM.
回答2:
There is no official way of doing so.
Yet, it is not to difficult to rebundle the code and run it on another VM via a Java agent. This would however not work as well as you expect it as it is unclear what class loader the shell should use and how it should interact with the running program.
回答3:
Answer https://stackoverflow.com/a/48132747/1561345 includes a hacky solution and a suggestion, what might the clean solution be.
The part of a another answer suggesting that JShell runs the code only in its VM is wrong - JShell launches a separate JVM with transport via JDI by default (at least on Linux). But yes, I don't know of a official way how to do it.
来源:https://stackoverflow.com/questions/44929943/can-the-java-9-jshell-be-used-to-run-code-in-another-jvm