Java Process exitValue() 方法



描述

Java Process exitValue() 方法返回子程序的退出值。

宣告

以下是 java.lang.Process.exitValue() 方法的宣告

public abstract int exitValue()

引數

返回值

此方法返回由該 Process 物件表示的子程序的退出值。按照慣例,值 0 表示正常終止。

異常

IllegalThreadStateException − 如果由該 Process 物件表示的子程序尚未終止。

檢查記事本程序的退出值示例

以下示例演示了 Process exitValue() 方法的使用。我們為記事本可執行檔案建立了一個 Process 物件。然後使用 destroy() 方法殺死記事本程序,並使用 exitValue() 方法列印退出值。

package com.tutorialspoint;

public class ProcessDemo {

   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         String[] cmds = {"notepad.exe"};
         Process p = Runtime.getRuntime().exec(cmds);

         // destroy the process instantly to get a exit value
         p.destroy();

         // get the exit value of the new process
         System.out.println("" + p.exitValue());

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Creating Process...
1

檢查計算器程序的退出值示例

以下示例演示了 Process exitValue() 方法的使用。我們為計算器可執行檔案建立了一個 Process 物件。然後使用 destroy() 方法殺死計算器程序,並使用 exitValue() 方法列印退出值。

package com.tutorialspoint;

public class ProcessDemo {

   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         String[] cmds = {"calc.exe"};
         Process p = Runtime.getRuntime().exec(cmds);

         // destroy the process instantly to get a exit value
         p.destroy();

         // get the exit value of the new process
         System.out.println("" + p.exitValue());

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Creating Process...
1

檢查 Windows 資源管理器程序的退出值示例

以下示例演示了 Process exitValue() 方法的使用。我們為 Windows 資源管理器可執行檔案建立了一個 Process 物件。然後使用 destroy() 方法殺死 Windows 資源管理器程序,並使用 exitValue() 方法列印退出值。

package com.tutorialspoint;

public class ProcessDemo {

   public static void main(String[] args) {
      try {
         // create a new process
         System.out.println("Creating Process...");
         String[] cmds = {"explorer.exe"};
         Process p = Runtime.getRuntime().exec(cmds);

         // destroy the process instantly to get a exit value
         p.destroy();

         // get the exit value of the new process
         System.out.println("" + p.exitValue());

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Creating Process...
1
java_lang_process.htm
廣告
© . All rights reserved.