简体   繁体   English

用于运行应用程序的Java 9 REPL

[英]Java 9 REPL for running app

Java 9 is introducing REPL called JShell inside the JDK distribution. Java 9在JDK发行版中引入了称为JShell的 REPL。 Is there any way to connect into the JShell of the JDK that is running some application and execute commands referencing that running app. 有什么方法可以连接到正在运行某些应用程序的JDK的JShell中,并执行引用该正在运行的应用程序的命令。 For example executing some methods form the apps code, inspecting objects, etc..? 例如,执行应用程序代码中的某些方法,检查对象等。

JShell is not the standard java command, it's another command. JShell不是标准的Java命令,它是另一个命令。 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. 键入命令后,shell会读取并解析该命令,并对其求值并打印结果。

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. 您可以定义变量并向类路径中添加类,但是我认为不可能从那里启动应用程序或服务器,至少在现阶段并且不在同一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). 但是,您可以在JShell范围中看到实例化的变量(创建的对象只能访问和打印其公共方法和变量)。

It seems JShell will have an api, which opens the way for other possibilities. 看来JShell将具有一个api,这为其他可能性开辟了道路。

No, not easily with jshell. 不,使用jshell不容易。

but let me rephrase your question so that we don't use jshell but whatever REPL java tool: 但是让我重新表述您的问题,以便我们不使用jshell而是使用任何REPL java工具:

Is there any way to connect some REPL java tool against a running java application and execute commands referencing that running app. 有什么方法可以将某些REPL Java工具连接到正在运行的Java应用程序并执行引用该正在运行的应用程序的命令。 For example executing some methods form the apps code, inspecting objects, etc..? 例如,执行应用程序代码中的某些方法,检查对象等。

https://github.com/lorenzoongithub/nudge4j https://github.com/lorenzoongithub/nudge4j

then you should take a look at nudge4j it does allow exactly that 那么你应该看看nudge4j它确实允许

the only caveat is for java 8. 唯一需要注意的是Java 8。

The alternative option that you could consider is something like BeanShell (although BeanShell is quite old and probably deprecated). 您可以考虑的替代选项是类似BeanShell的东西(尽管BeanShell相当老,并且可能已弃用)。 You can launch a BeanShell console from inside your application that would allow you to access your objects and call methods on them. 您可以从应用程序内部启动BeanShell控制台,该控制台将允许您访问对象并在其上调用方法。 See the JConsole example for some ideas. 有关一些想法,请参见JConsole示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM