開心生活站

位置:首頁 > IT科技 > 

java,process

IT科技2.58W

<link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

java process是什麼?一起來看看小編今天的分享吧!

在項目開發中,經常會遇到調用其它程序功能的業務需求,在java中通常有兩種實現方法,包括:

Runtime runtime = Runtime.getRuntime();Process p =runtime.exec(cmd);

Process p=new ProcessBuilder(cmd).start();

在這裏就需要認識一下process類,process是一個抽象的類,它包含6個抽象的方法:

abstract  voiddestroy()           殺掉子進程。abstract  intexitValue()           返回子進程的出口值。abstract  InputStreamgetErrorStream()           獲取子進程的錯誤流。abstract  InputStreamgetInputStream()           獲取子進程的輸入流。abstract  OutputStreamgetOutputStream()           獲取子進程的輸出流。abstract  intwaitFor()           導致當前線程等待,如有必要,一直要等到由該 Process 對象表示的進程已經終止。

java process

以“ping 百度網址”爲例:

import java.io.IOException; public class ProcessDemo {        public static void main(String[] args) {                try {                         Process process = Runtime.getRuntime().exec("ping www.baidu.com");                         System.out.println("任務執行完畢!");              } catch (IOException e) {                                       // TODO Auto-generated catch block                                       e.printStackTrace();              }       }}

由於Process提供了WaitFor和getInputStream兩個方法,這兩個方法都阻塞java線程,使調用ping被掛到後臺運行,程序直接打印“任務執行完畢!”,而我們想要的效果是先把ping操作執行完成後,再輸出“任務執行完畢!”,可以等待腳本返回或結束後,再繼續執行java程序:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader; public class ProcessDemo {         public static void main(String[] args) {                   try {                        Process process = Runtime.getRuntime().exec("ping www.baidu.com");                        BufferedReader bufferedReader = new BufferedReader(                                  new InputStreamReader(process.getInputStream(),"gbk"));                        String line;                        while ((line = bufferedReader.readLine()) != null) {                            System.out.println(line);                        }                        System.out.println("任務執行完畢!");                     } catch (IOException e) {                        // TODO Auto-generated catch block                       e.printStackTrace();                      }           }}
import java.io.IOException; public class ProcessDemo {     public static void main(String[] args) {          try {              Process process = Runtime.getRuntime().exec("ping www.baidu.com");              process.waitFor();              System.out.println("任務執行完畢!");          } catch (IOException e) {              // TODO Auto-generated catch block              e.printStackTrace();          } catch (InterruptedException e) {                                                          // TODO Auto-generated catch block              e.printStackTrace();          }     }}

這樣就完成了process的Java程序操作。

標籤:java Process