How to execute a javascript with jshell?

后端 未结 4 1004
自闭症患者
自闭症患者 2020-12-08 08:07

Given that Java 9 is upon us and we can finally have a java REPL with jshell I was hoping there was a way to add a shebang to a script and have jshell

4条回答
  •  轮回少年
    2020-12-08 08:56

    It turns out that with a bit of trickery there is a way, although I haven't fully managed to suppress the interpreted commands but pretty close to what I want.

    Change test.jsh to:

    #!/usr/bin/env sh
    tail -n +4 "$0" | jshell -s "$@"
    exit $?
    System.out.println("Hello World")
    /exit
    

    Which gives us:

    ⚡ ./test.jsh
    -> System.out.println("Hello World")
    Hello World
    -> /exit
    

提交回复
热议问题