简体   繁体   English

如何从Shell或Windows CommandLine以内联方式执行Java jshell命令

[英]How to execute java jshell command as inline from shell or windows commandLine

Are there any way to execute java command on REPL ( jshell ) as inline command without launching it? 有什么方法可以在REPL( jshelljshell Java命令作为内联命令执行而无需启动它?

Eg Perl inline command 例如,Perl内联命令

$perl -e 'printf("%06d", 19)'
000019

I have to launch jshell to run any command: 我必须启动jshell来运行任何命令:

$jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro
jshell> String.format("%06d", 19)
$1 ==> "000019"

I found similar question here , but it's not feasible solution to create a separate jsh file for individual command. 我在这里发现了类似的问题,但为单个命令创建单独的jsh文件不是可行的解决方案。

Another solution on same post is: echo "1+2"|jshell 同一帖子上的另一个解决方案是: echo "1+2"|jshell

temp=`echo 'String.format("%06d", 19)'|jshell`
echo $temp

Ouch output 哎呀输出

| Welcome to JShell -- Version 9 | For an introduction type: /help intro jshell> String.format("%06d", 19) $1 ==> "000019" jshell>

I am expecting $temp only print 000019 . 我期望$temp仅打印000019

By default the normal feedback mode is used in your interaction with JShell and this mode prints command output, Declaration, Commands and Prompt. 默认情况下,在与JShell交互时使用normal反馈模式,该模式将打印命令输出,声明,命令和提示。 Read Introduction to jshell feedback modes for more details. 阅读jshell反馈模式简介以获取更多详细信息。

Steps to get command output: 获取命令输出的步骤:

  1. Run jshell in concise feedback mode to skip Command and Declaration details, it will still print prompt and value. concise反馈模式下运行jshell可以跳过“命令”和“声明”详细信息,它仍会显示提示和值。

      $ echo 'String.format("%06d", 19)' | jshell --feedback concise jshell> String.format("%06d", 19) $1 ==> "000019" 
  2. Filter 2nd row from result using sed - 使用sed从结果中过滤第二行-

     echo 'String.format("%06d", 19)' | jshell --feedback concise | sed -n '2p' 
  3. Extract only value and exclude other details- 仅提取值并排除其他详细信息-

     echo 'String.format("%06d", 19)' | jshell --feedback concise | sed -n '2p' |sed -En 's/[^>]*>(.+)/\\1/gp' 

如果使用System.out.println,则不需要第二个sed

echo "System.out.println(\"$JAVA_HOME\");" | jshell --feedback concise | sed -n '2p'

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

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