using jshell in executable UNIX scripts

Paul Sandoz paul.sandoz at oracle.com
Tue Oct 11 19:26:55 UTC 2016


Hi Peter,

I was wondering when this would be discussed :-)

This potentially starts moving down the slippery slope of what it means to be Java syntax and what it means to execute. Larger topics to be thought about.

Here is some interesting discussion about Go:

  http://golangcookbook.com/chapters/running/shebang/

Surprisingly you can make it work in bash (from the above link):

  //$JDK_HOME/bin/jshell $0 $@; exit $?
  System.out.println("HELLO");

  /exit

Paul.

> On 10 Oct 2016, at 04:03, Peter Levart <peter.levart at gmail.com> wrote:
> 
> Hi,
> 
> I don't know if this is the right list to discuss this, so please direct me to a more suitable place if there is one.
> 
> "jshell" command is a very nice interactive Java shell, but it could also be used for scripting. An executable script in any major UNIX OS is a textual file with executable permissions that starts with the following two characters: #!
> The rest of the 1st line is the path to the interpreter executable and any arguments passed to it. The last argument passed to the interpreter is the path to the executable script. In case of jshell, one would want such script to be written like:
> 
> #!/home/peter/Apps64/jdk9/bin/jshell
> 
> System.out.println("Hello World!");
> 
> /exit
> 
> 
> The problem is that jshell tries to parse the 1st line using jshell syntax and the result of running above executable script is:
> 
> | Error:
> |  illegal character: '#'
> | #!/home/peter/Apps64/jdk9/bin/jshell
> |  ^
> |  Error:
> |  illegal start of expression
> |  #!/home/peter/Apps64/jdk9/bin/jshell
> |    ^
> Hello World!
> 
> 
> The script is actually executed, but the syntax error encountered in the 1st line is printed too.
> 
> Would it be possible for jshell to skip 1st line if it starts with characters #! like other shells do?
> 
> 
> Regards, Peter
> 



More information about the core-libs-dev mailing list