Reputation: 9301
Java 9 is introducing REPL called JShell inside the JDK distribution. Is there any way to connect into the JShell of the JDK that is running some application and execute commands referencing that running app. For example executing some methods form the apps code, inspecting objects, etc..?
Upvotes: 10
Views: 969
Reputation: 1392
JShell is not the standard java command, it's another command. Once you open the shell it "goes" in Read Eval Print Loop. Once you type a command it's read and parsed by the shell, evaluated and the result is printed.
You can define variables and add classes to the classpath, but I don't think is possible starting an application or a server from there, at least not at this stage and not in the same JVM. As you can add classes you can instantiate them, but you don't have an option to inspect their variables. However you can see the variables you instantiate in your JShell scope (what objects you created, you can access and print only their their public methods and variables).
It seems JShell will have an api, which opens the way for other possibilities.
Upvotes: 4
Reputation: 455
The alternative option that you could consider is something like BeanShell (although BeanShell is quite old and probably deprecated). You can launch a BeanShell console from inside your application that would allow you to access your objects and call methods on them. See the JConsole example for some ideas.
Upvotes: 0
Reputation: 15305
No, not easily with jshell.
but let me rephrase your question so that we don't use jshell but whatever REPL java tool:
Is there any way to connect some REPL java tool against a running java application and execute commands referencing that running app. For example executing some methods form the apps code, inspecting objects, etc..?
https://github.com/lorenzoongithub/nudge4j
then you should take a look at nudge4j it does allow exactly that
the only caveat is for java 8.
Upvotes: 0