如何在 Java 9 中將原始碼載入到 JShell 中?


JShell 是一款用於學習 Java 的互動工具,它是能夠評估宣告、語句和表示式的REPL(Read-Evaluate-Print-Loop)

離開 JShell 會話時,我們想要將之前輸入的新會話程式碼重用。這可以透過使用命令/open [File_Path]來完成。此命令將載入選項中提供的file[File_Path]中找到的所有程式碼和內部命令。

在下面的程式碼片段中,我們可以使用 "/open [File_Path]"命令,從副檔名為".jsh"的目錄中載入原始碼。

C:\Users\User>jshell
|  Welcome to JShell -- Version 9.0.4
|  For an introduction type: /help intro

jshell> x
|  Error:
|  cannot find symbol
|   symbol: variable x
|  x
|  ^

jshell> y
|  Error:
|  cannot find symbol
|   symbol: variable y
|  y
|  ^

jshell> /list

jshell> /open C:\Users\User\Desktop\save.jsh

jshell> /list

  1 : int x = 15;
  2 : double y = 25.0;
  3 : public int sum(int a, int b) {
         return a + b;
      }

jshell> x
x ==> 15

jshell> y
y ==> 25.0

jshell> int result = sum(5, 7)
result ==> 12

更新於:07-4-2020

238 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
Advertisement
© . All rights reserved.