1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100107.html;106,k100110.html;107,k100112.html;108,k100114.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100107.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100110.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100112.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100114.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 16 - Utility-Klassen I
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id016004"></a>
<h2>16.4 Die Klasse RunTime </h2>
<hr>
<ul>
<li><a href="k100111.html#sectlevel2id016004">16.4 Die Klasse RunTime</a>
<ul>
<li><a href="k100111.html#sectlevel3id016004001">16.4.1 Grundlagen</a>
<li><a href="k100111.html#sectlevel3id016004002">16.4.2 Interaktion mit dem externen Programm</a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id016004001"></a>
<h3>16.4.1 Grundlagen </h3>
<p>
Die Klasse <a name="ixa101125"><a href="index_r.html#ixb100859"><font color=#000080><tt>Runtime</tt></font></a></a>
des Pakets <a href="index_j.html#ixb100188"><font color=#000080><tt>java.lang</tt></font></a>
ermöglicht die Interaktion eines Java-Programms mit dem Bestandteil
seiner Laufzeitumgebung, der dafür verantwortlich ist, Programme
zu starten und zu beenden. Dabei ist es insbesondere möglich,
mit seiner Hilfe externe Programme und Kommandos zu starten, mit ihnen
zu kommunizieren und ihren Zustand zu überwachen.
<p>
Objekte der Klasse <a href="index_r.html#ixb100859"><font color=#000080><tt>Runtime</tt></font></a>
dürfen vom Programm nicht selbst instanziert werden, sondern
werden über die statische Methode <a name="ixa101126"><a href="index_g.html#ixb100860"><font color=#000080><tt>getRuntime</tt></font></a></a>
der Klasse <a href="index_r.html#ixb100859"><font color=#000080><tt>Runtime</tt></font></a>
beschafft:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public static Runtime getRuntime()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/lang/Runtime.html" onClick="this.href=getApiDoc('java.lang.Runtime')"><font color="#660066" size=-1>java.lang.Runtime</font></a></td>
</tr>
</table>
<p>
Um ein externes Programm zu starten, gibt es vier Methoden mit dem
Namen <a name="ixa101127"><a href="index_e.html#ixb100861"><font color=#000080><tt>exec</tt></font></a></a>:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public Process exec(String command)
throws IOException
public Process exec(String command, String[] envp)
throws IOException
public Process exec(String[] cmdarray)
throws IOException
public Process exec(String[] cmdarray, String[] envp)
throws IOException
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/lang/Runtime.html" onClick="this.href=getApiDoc('java.lang.Runtime')"><font color="#660066" size=-1>java.lang.Runtime</font></a></td>
</tr>
</table>
<p>
Das auszuführende Kommando kann wahlweise als Einzelstring angegeben
werden (Kommandoname plus Argumente, getrennt durch Leerzeichen),
oder die einzelnen Bestandteile können in einem Array übergeben
werden. Das optionale zweite Argument <font color="#000077"><tt>envp</tt></font>
ermöglicht es, dem zu startenden Programm eine Liste von Umgebungsvariablen
zu übergeben.
<p>
Das folgende Listing zeigt die einfachste Form der Anwendung von <a href="index_e.html#ixb100861"><font color=#000080><tt>exec</tt></font></a>.
Das Programm startet den <i>notepad</i>-Editor unter Windows:
<a name="listingid016008"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing1608.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.io.*;
<font color="#555555">004 </font>
<font color="#555555">005 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing1608
<font color="#555555">006 </font>{
<font color="#555555">007 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">008 </font> {
<font color="#555555">009 </font> <font color="#0000AA">try</font> {
<font color="#555555">010 </font> Runtime.getRuntime().exec(<font color="#0000FF">"notepad"</font>);
<font color="#555555">011 </font> } <font color="#0000AA">catch</font> (Exception e) {
<font color="#555555">012 </font> System.err.println(e.toString());
<font color="#555555">013 </font> }
<font color="#555555">014 </font> }
<font color="#555555">015 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing1608.java"><font color="#000055" size=-1>Listing1608.java</font></a></td>
</tr>
</table>
<i>
Listing 16.8: Starten von notepad.exe</i></p>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Leider macht die API-Dokumentation keine Angaben darüber, in
welchen Verzeichnissen nach dem auszuführenden Programm gesucht
wird. In den aktuellen Implementierungen des JDK scheint es jedoch
so zu sein, dass alle Verzeichnisse durchsucht werden, die in der
<font color="#000077"><tt>PATH</tt></font>-Umgebungsvariablen angegeben
sind. Unter Windows werden dabei aber offensichtlich nur Dateien mit
den Erweiterungen <font color="#660099">.com</font> und <font color="#660099">.exe</font>
automatisch berücksichtigt. Soll dagegen eine <font color="#660099">.bat</font>-Datei
gestartet werden, muss die Erweiterung <font color="#660099">.bat</font>
explizit angegeben werden.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel3id016004002"></a>
<h3>16.4.2 Interaktion mit dem externen Programm </h3>
<p>
<a href="index_e.html#ixb100861"><font color=#000080><tt>exec</tt></font></a>
gibt ein Objekt des Typs <a name="ixa101128"><a href="index_p.html#ixb100862"><font color=#000080><tt>Process</tt></font></a></a>
zurück, das dazu verwendet werden kann, mit dem gestarteten Programm
zu interagieren. Dazu definiert <a href="index_p.html#ixb100862"><font color=#000080><tt>Process</tt></font></a>
folgende Methoden:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public abstract int waitFor()
throws InterruptedException
public abstract int exitValue()
public abstract void destroy()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/lang/Process.html" onClick="this.href=getApiDoc('java.lang.Process')"><font color="#660066" size=-1>java.lang.Process</font></a></td>
</tr>
</table>
<p>
Ein Aufruf von <a name="ixa101129"><a href="index_w.html#ixb100863"><font color=#000080><tt>waitFor</tt></font></a></a>
terminiert erst, wenn der zugehörige Prozess beendet wurde. Diese
Methode kann also dazu verwendet werden, auf das Ende des gestarteten
Programms zu warten. Das ist beispielsweise sinnvoll, um den Rückgabewert
des Programms auszuwerten oder um mit von ihm erzeugten oder veränderten
Daten zu arbeiten. Wird <a href="index_w.html#ixb100863"><font color=#000080><tt>waitFor</tt></font></a>
nicht aufgerufen, kann auf aktuellen Betriebssystemen wohl davon ausgegangen
werden, dass das externe Programm parallel zum Java-Programm gestartet
wird und asynchron weiterläuft. Diese Aussage ist allerdings
mit Vorsicht zu genießen, denn spezifiziert ist dieses Verhalten
nicht. Im Zweifel hilft ausprobieren.
<p>
Nach Ende des externen Programms kann mit <a name="ixa101130"><a href="index_e.html#ixb100864"><font color=#000080><tt>exitValue</tt></font></a></a>
sein Rückgabewert abgefragt werden. Dieser gibt bei vielen Programmen
an, ob es fehlerfrei ausgeführt werden konnte oder nicht. Das
ist aber nicht zwangsläufig so. Unter Windows wird insbesondere
beim Aufruf von Batch-Dateien und bei der expliziten Ausführung
eines Kommandos in einen eigenen Kommandointerpreter der Rückgabewert
nicht weitergegeben. In diesem Fall liefert <a href="index_e.html#ixb100864"><font color=#000080><tt>exitValue</tt></font></a>
immer den Wert 0. Wird <a href="index_e.html#ixb100864"><font color=#000080><tt>exitValue</tt></font></a>
aufgerufen, wenn der Prozess noch läuft, gibt es eine <a name="ixa101131"><a href="index_i.html#ixb100865"><font color=#000080><tt>IllegalThreadStateException</tt></font></a></a>.
<p>
Die Methode <a name="ixa101132"><a href="index_d.html#ixb100866"><font color=#000080><tt>destroy</tt></font></a></a>
dient dazu, das externe Programm abzubrechen. Es handelt sich hierbei
nicht um das normale Beenden eines Programms, sondern um einen harten
Abbruch. Ungesicherte Änderungen gehen also verloren und es können
Inkonsistenzen in manipulierten Daten entstehen.
<p>
Das <a href="index_p.html#ixb100862"><font color=#000080><tt>Process</tt></font></a>-Objekt
bietet zusätzlich die Möglichkeit, die Standardein- und
-ausgabe des externen Kommandos umzuleiten und aus dem eigenen Programm
heraus anzusprechen:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
public OutputStream getOutputStream()
public abstract InputStream getInputStream()
public abstract InputStream getErrorStream()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/lang/Process.html" onClick="this.href=getApiDoc('java.lang.Process')"><font color="#660066" size=-1>java.lang.Process</font></a></td>
</tr>
</table>
<p>
<a name="ixa101133"><a href="index_g.html#ixb100671"><font color=#000080><tt>getInputStream</tt></font></a></a>
und <a name="ixa101134"><a href="index_g.html#ixb100867"><font color=#000080><tt>getErrorStream</tt></font></a></a>
liefern einen <a href="index_i.html#ixb100642"><font color=#000080><tt>InputStream</tt></font></a>,
mit dem die Ausgaben des Prozesses auf Standardausgabe und Standardfehler
gelesen werden können. Von <a name="ixa101135"><a href="index_g.html#ixb100672"><font color=#000080><tt>getOutputStream</tt></font></a></a>
wird ein <a href="index_o.html#ixb100673"><font color=#000080><tt>OutputStream</tt></font></a>
zur Verfügung gestellt, mit dem Daten in die Standardeingabe
des Prozesses geschrieben werden können. Auf diese Weise lassen
sich Programme, die über Standardein- und -ausgabe kommunizieren,
fernsteuern bzw. fernabfragen.
<p>
Das folgende Programm fasst die wichtigsten Möglichkeiten zusammen:
<a name="runcommand"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* RunCommand.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.io.*;
<font color="#555555">004 </font>
<font color="#555555">005 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> RunCommand
<font color="#555555">006 </font>{
<font color="#555555">007 </font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> <font color="#006699">int</font> MODE_UNCONNECTED = 0;
<font color="#555555">008 </font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> <font color="#006699">int</font> MODE_WAITFOR = 1;
<font color="#555555">009 </font> <font color="#0000AA">static</font> <font color="#0000AA">final</font> <font color="#006699">int</font> MODE_CATCHOUTPUT = 2;
<font color="#555555">010 </font>
<font color="#555555">011 </font> <font color="#0000AA">private</font> <font color="#0000AA">static</font> <font color="#006699">void</font> runCommand(String cmd, <font color="#006699">int</font> mode)
<font color="#555555">012 </font> <font color="#0000AA">throws</font> IOException
<font color="#555555">013 </font> {
<font color="#555555">014 </font> Runtime rt = Runtime.getRuntime();
<font color="#555555">015 </font> System.out.println(<font color="#0000FF">"Running "</font> + cmd);
<font color="#555555">016 </font> Process pr = rt.exec(cmd);
<font color="#555555">017 </font> <font color="#0000AA">if</font> (mode == MODE_WAITFOR) {
<font color="#555555">018 </font> System.out.println(<font color="#0000FF">"waiting for termination"</font>);
<font color="#555555">019 </font> <font color="#0000AA">try</font> {
<font color="#555555">020 </font> pr.waitFor();
<font color="#555555">021 </font> } <font color="#0000AA">catch</font> (InterruptedException e) {
<font color="#555555">022 </font> }
<font color="#555555">023 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (mode == MODE_CATCHOUTPUT) {
<font color="#555555">024 </font> System.out.println(<font color="#0000FF">"catching output"</font>);
<font color="#555555">025 </font> BufferedReader procout = <font color="#0000AA">new</font> BufferedReader(
<font color="#555555">026 </font> <font color="#0000AA">new</font> InputStreamReader(pr.getInputStream())
<font color="#555555">027 </font> );
<font color="#555555">028 </font> String line;
<font color="#555555">029 </font> <font color="#0000AA">while</font> ((line = procout.readLine()) != <font color="#006699">null</font>) {
<font color="#555555">030 </font> System.out.println(<font color="#0000FF">" OUT> "</font> + line);
<font color="#555555">031 </font> }
<font color="#555555">032 </font> }
<font color="#555555">033 </font> <font color="#0000AA">try</font> {
<font color="#555555">034 </font> System.out.println(
<font color="#555555">035 </font> <font color="#0000FF">"done, return value is "</font> + pr.exitValue()
<font color="#555555">036 </font> );
<font color="#555555">037 </font> } <font color="#0000AA">catch</font> (IllegalThreadStateException e) {
<font color="#555555">038 </font> System.out.println(
<font color="#555555">039 </font> <font color="#0000FF">"ok, process is running asynchronously"</font>
<font color="#555555">040 </font> );
<font color="#555555">041 </font> }
<font color="#555555">042 </font> }
<font color="#555555">043 </font>
<font color="#555555">044 </font> <font color="#0000AA">private</font> <font color="#0000AA">static</font> <font color="#006699">void</font> runShellCommand(String cmd, <font color="#006699">int</font> mode) <a name="runcommand.a"></a>
<font color="#555555">045 </font> <font color="#0000AA">throws</font> IOException
<font color="#555555">046 </font> {
<font color="#555555">047 </font> String prefix = <font color="#0000FF">""</font>;
<font color="#555555">048 </font> String osName = System.getProperty(<font color="#0000FF">"os.name"</font>);
<font color="#555555">049 </font> osName = osName.toLowerCase();
<font color="#555555">050 </font> <font color="#0000AA">if</font> (osName.indexOf(<font color="#0000FF">"windows"</font>) != -1) {
<font color="#555555">051 </font> <font color="#0000AA">if</font> (osName.indexOf(<font color="#0000FF">"95"</font>) != -1) {
<font color="#555555">052 </font> prefix = <font color="#0000FF">"command.com /c "</font>;
<font color="#555555">053 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (osName.indexOf(<font color="#0000FF">"98"</font>) != -1) {
<font color="#555555">054 </font> prefix = <font color="#0000FF">"command.com /c "</font>;
<font color="#555555">055 </font> }
<font color="#555555">056 </font> }
<font color="#555555">057 </font> <font color="#0000AA">if</font> (prefix.length() <= 0) {
<font color="#555555">058 </font> System.out.println(
<font color="#555555">059 </font> <font color="#0000FF">"unknown OS: don\'t know how to invoke shell"</font>
<font color="#555555">060 </font> );
<font color="#555555">061 </font> } <font color="#0000AA">else</font> {
<font color="#555555">062 </font> runCommand(prefix + cmd, mode);
<font color="#555555">063 </font> }
<font color="#555555">064 </font> }
<font color="#555555">065 </font>
<font color="#555555">066 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">067 </font> {
<font color="#555555">068 </font> <font color="#0000AA">try</font> {
<font color="#555555">069 </font> <font color="#0000AA">if</font> (args.length <= 0) {
<font color="#555555">070 </font> System.out.println(
<font color="#555555">071 </font> <font color="#0000FF">"Usage: java RunCommand [-shell] "</font> +
<font color="#555555">072 </font> <font color="#0000FF">"[-waitfor|-catchoutput] <command>"</font>
<font color="#555555">073 </font> );
<font color="#555555">074 </font> System.exit(1);
<font color="#555555">075 </font> }
<font color="#555555">076 </font> <font color="#006699">boolean</font> shell = <font color="#006699">false</font>;
<font color="#555555">077 </font> <font color="#006699">int</font> mode = MODE_UNCONNECTED;
<font color="#555555">078 </font> String cmd = <font color="#0000FF">""</font>;
<font color="#555555">079 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < args.length; ++i) {
<font color="#555555">080 </font> <font color="#0000AA">if</font> (args[i].startsWith(<font color="#0000FF">"-"</font>)) {
<font color="#555555">081 </font> <font color="#0000AA">if</font> (args[i].equals(<font color="#0000FF">"-shell"</font>)) {
<font color="#555555">082 </font> shell = <font color="#006699">true</font>;
<font color="#555555">083 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (args[i].equals(<font color="#0000FF">"-waitfor"</font>)) {
<font color="#555555">084 </font> mode = MODE_WAITFOR;
<font color="#555555">085 </font> } <font color="#0000AA">else</font> <font color="#0000AA">if</font> (args[i].equals(<font color="#0000FF">"-catchoutput"</font>)) {
<font color="#555555">086 </font> mode = MODE_CATCHOUTPUT;
<font color="#555555">087 </font> }
<font color="#555555">088 </font> } <font color="#0000AA">else</font> {
<font color="#555555">089 </font> cmd = args[i];
<font color="#555555">090 </font> }
<font color="#555555">091 </font> }
<font color="#555555">092 </font> <font color="#0000AA">if</font> (shell) {
<font color="#555555">093 </font> runShellCommand(cmd, mode);
<font color="#555555">094 </font> } <font color="#0000AA">else</font> {
<font color="#555555">095 </font> runCommand(cmd, mode);
<font color="#555555">096 </font> }
<font color="#555555">097 </font> } <font color="#0000AA">catch</font> (Exception e) {
<font color="#555555">098 </font> System.err.println(e.toString());
<font color="#555555">099 </font> }
<font color="#555555">100 </font> }
<font color="#555555">101 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/RunCommand.java"><font color="#000055" size=-1>RunCommand.java</font></a></td>
</tr>
</table>
<i>
Listing 16.9: Starten externer Programme</i></p>
<p>
Das Hauptprogramm erwartet das zu startende Programm und seine Parameter
als Argumente. Wird die Option »-catchoutput« angegeben,
liest das Programm die Ausgaben des gestarteten Programms und gibt
sie auf seiner eigenen Standardausgabe aus. Wird »-waitfor«
angegeben, wartet das Programm auf das Ende des gestarteten Programms,
ohne dessen Ausgaben anzuzeigen. In beiden Fällen wird schließlich
der Rückgabewert des Programms ausgegeben. Durch Angabe der Option
»-shell« kann das externe Programm mit einem separaten Kommandointerpreter
gestartet werden. Das ist beispielsweise nützlich, um Shell-Kommandos
auszuführen, die nicht als eigenständige Programmdateien
existieren.
<p>
Der folgende Aufruf verwendet das Beispielprogramm, um das interne
MS-DOS-Kommando »set« auszuführen (es gibt die Inhalte
aller Umgebungsvariablen aus):
<font color="#333300">
<pre>
java RunCommand -shell -catchoutput set
</pre>
</font>
<p>
Seine Ausgabe könnte etwa so aussehen:
<font color="#333300">
<pre>
Running command.com /c set
catching output
OUT> winbootdir=C:\WINDOWS
OUT> COMSPEC=C:\COMMAND.COM
OUT> TEMP=C:\tmp
OUT> TMP=c:\tmp
OUT> USER=guido
OUT> windir=C:\WINDOWS
OUT> PATH=C:\JDK1.6\BIN;C:\WINDOWS;C:\WINDOWS\COMMAND;
OUT> CLASSPATH=.;c:\arc\prog\java
OUT> PROMPT=$p--$g
done, return value is 0
</pre>
</font>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Die Übergabe des Programms an einen separaten Kommandointerpreter
erfolgt in der Methode <font color="#000077"><tt>runShellCommand</tt></font>
ab <a href="k100111.html#runcommand.a">Zeile 044</a>. Wie ein derartiger
Aufruf ausgeführt wird, ist natürlich betriebssystem- und
konfigurationsabhängig. Das Beispielprogramm versucht, einige
brauchbare Varianten für gängige Betriebssysteme vorzudefinieren.
Weitere können leicht hinzugefügt werden.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Während des Tests von <font color="#000077"><tt>RunCommand</tt></font>
gab es mitunter Schwierigkeiten beim Ausführen interner DOS-Programme
unter Windows 95 und 98. Während sich beispielsweise das Kommando
<font color="#000077"><tt>set</tt></font> problemlos aufrufen ließ,
gab es beim Aufruf von <font color="#000077"><tt>dir</tt></font> Hänger,
nach denen die MS-DOS-Task hart abgebrochen werden mußte. Die
JDK Bug Database listet eine ganze Reihe von Problemen in Zusammenhang
mit dem Aufruf von 16-Bit-Programmen unter Windows 95 oder 98 auf.
Sie rühren unter anderem daher, dass die Ein- und Ausgabepuffer
der DOS-Programme so klein sind, dass die Programme mitunter schon
blockieren, bevor die aufrufende Applikation die Chance hatte, eine
Verbindung zu ihnen herzustellen. Echte Workarounds für diese
Probleme scheinen nicht bekannt zu sein. Beim Aufruf von 32-Bit-Programmen
treten die Probleme offenbar nicht auf.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#CC0000">
<tr>
<td><font color="#FFFFFF"> Warnung </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
</tr>
</table>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100107.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100110.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100112.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100114.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|