How to run a JShell File?

前端 未结 7 919
借酒劲吻你
借酒劲吻你 2020-12-19 00:08

I\'d like to run an entire file with JShell like:

$ jshell my-jshell-skript.java

Where e.g. the content of my my-jshell-skript.java

7条回答
  •  醉梦人生
    2020-12-19 00:29

    In jshell you can save the current snippets into a file by issuing:

    /save Filename
    

    Likewise, you can load the file into the current context/session by issuing:

    /open Filename
    

    Here is one such example:

    |  Welcome to JShell -- Version 9.0.7.1
    |  For an introduction type: /help intro
    
    jshell> String[] names={"nameone","nametwo"}
    names ==> String[2] { "nameone", "nametwo" }
    
    jshell> Arrays.toString(names);
    $2 ==> "[nameone, nametwo]"
    
    jshell> /save myExample
    
    jshell> %                                                                                                                                             sudipbhandari at sysadm-Latitude-5480 in ~                                                                                                      18:22
    > jshell
    |  Welcome to JShell -- Version 9.0.7.1
    |  For an introduction type: /help intro
    
    jshell> names
    |  Error:
    |  cannot find symbol
    |    symbol:   variable names
    |  names
    |  ^---^
    
    jshell> /open myExample
    
    jshell> names
    names ==> String[2] { "nameone", "nametwo" }
    

提交回复
热议问题