File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Title : Java에서의 프로세스 생성
3
+ * Date : 2019-01-13
4
+ * Author : Donald D
5
+ * Detail : An instance of JVM은 다중 스레드는 지원하지만 process 생성은 지원하지 않는다.
6
+ * 하나의 JVM(= 하나의 프로세스, = 하나의 주소 공간)에서 process memory(주소 공간)들을 분리하기 어렵기 때문이다.
7
+ * => Possible to create a process external to the JVM by using ProcessBuilder class
8
+ * processBuilder allows a java program to specify a OS-native process (such as Unix /usr/bin/ls or C:\\windows\\system32\\mspaint.exe)
9
+ */
10
+
11
+ import java .io .BufferedReader ;
12
+ import java .io .IOException ;
13
+ import java .io .InputStream ;
14
+ import java .io .InputStreamReader ;
15
+
16
+ public class OSProcess {
17
+ public static void main (String [] args ) throws IOException {
18
+ if (args .length != 1 ) {
19
+ System .err .println ("사용법: java OSProcess 명령" );
20
+ System .exit (0 );
21
+ }
22
+ ProcessBuilder pb = new ProcessBuilder (args [0 ]);
23
+ Process proc = pb .start (); //external process 생성 - 명령 실행
24
+ //external process와 JVM은 서로 영향을 줄 수 없다. 따라서 external process의 InputStream, OutputStream을 통해 통신한다.
25
+ InputStream is = proc .getInputStream (); //proc의 입력스트림
26
+ BufferedReader br = new BufferedReader (new InputStreamReader (is ));
27
+ String line ;
28
+ while ((line = br .readLine ()) != null ) { //proc의 출력을 읽음
29
+ System .out .println (line );
30
+ }
31
+ br .close ();
32
+ }
33
+ }
You can’t perform that action at this time.
0 commit comments