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

試著忘記壹切 提交于 2019-12-03 21:53:10

By default the normal feedback mode is used in your interaction with JShell and this mode prints command output, Declaration, Commands and Prompt. Read Introduction to jshell feedback modes for more details.

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.

     $ echo 'String.format("%06d", 19)' | jshell --feedback concise
       jshell> String.format("%06d", 19)
       $1 ==> "000019"
    
  2. Filter 2nd row from result using 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'
    

If you use System.out.println, you will not need the second sed

echo "System.out.println(\"$JAVA_HOME\");" | jshell --feedback concise | sed -n '2p'
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!