6

Are there any way to execute java command on REPL (jshell) as inline command without launching it?

E.g. 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.

Rahul Sharma
  • 5,614
  • 10
  • 57
  • 91
  • Possible duplicate of [How to run a JShell File?](https://stackoverflow.com/questions/46426526/how-to-run-a-jshell-file) – Naman Oct 14 '17 at 01:20
  • Thanks for suggestion. I checked the suggested post, it's not feasible to create a separate file for individual command. One can have `n` number of commands to execute at different places. – Rahul Sharma Oct 14 '17 at 01:35
  • [One of the answer](https://stackoverflow.com/a/46426620/1746118) on the same post suggests using `echo` and doesn't require the script though. Did you try that? *commands to execute at different places.* what does different places mean here? – Naman Oct 14 '17 at 01:36
  • I tried it but the response is not expected. please check post. – Rahul Sharma Oct 14 '17 at 01:48
  • I am trying $temp to assign only `3` and exclude rest of info. Is there any option to skip jshell to print version info? – Rahul Sharma Oct 14 '17 at 01:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156711/discussion-between-squid-and-nullpointer). – Rahul Sharma Oct 14 '17 at 01:56

2 Answers2

0

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'
    
Rahul Sharma
  • 5,614
  • 10
  • 57
  • 91
  • Sure is this working in a Windows CMD / PowerShell command line? I've been testing with Windows 10, JDK11 and not working: JShell opens but freezes on the `jshell>` command. Neither works with WSL. – lucasvc Jan 29 '20 at 09:11
-1

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'
Maruthi
  • 460
  • 4
  • 11