简体   繁体   中英

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?

Eg Perl inline command

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

I have to launch jshell to run any command:

$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.

Another solution on same post is: 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 .

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' 

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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