Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 4 5 6 7 8 9 < 10 > 11 12 13 14 15 16 .. 166 >> Next

Process proc = rt.exec("dir");
E:\classes\org\javapitfalls\item1>java org.javapitfalls.item1.BadExecWinDir java.io.IOException: CreateProcess: dir error=2
at java.lang.Win32Process.create(Native Method) at java.lang.Win32Process.<init>(Unknown Source) at java.lang.Runtime.execInternal(Native Method) at BadExecWinDir.main(BadExecWinDir.java:12)
When Runtime.exec() Won't 9
01 package org.javapitfalls.item1;
02
03 import java.util.*;
04 import java.io.*;
05
06 class StreamGobbler extends Thread
07 {
08 InputStream is;
09 String type;
10
11 StreamGobbler(InputStream is, String type)
12 {
13 this.is = is;
14 this.type = type;
15 }
16
17 public void run()
18 {
19 try
20 {
21 InputStreamReader isr = new InputStreamReader(is);
22 BufferedReader br = new BufferedReader(isr);
23 String line=null;
24 while ( (line = br.readLine()) != null)
25 {
26: System.out.println(type + ">" + line);
27 System.out.flush();
28 }
29 } catch (IOException ioe)
30 {
31 ioe.printStackTrace();
32 }
33 }
34 }
35
36 public class GoodWindowsExec
37 {
38 public static void main(String args[])
39 {
// ... command line check omitted for brevity ...
45
46 try
47 {
48 String osName = System.getProperty("os.name" );
49 System.out.println("osName: " + osName);
50 String[] cmd = new String[3];
Listing 1.4 GoodWindowsExec.java (continued)
10 Item 1
51
52 if(osName.equals("Windows NT") ||
53 osName.equals("Windows 2000"))
54 {
55 cmd[0] = "cmd.exe" ;
56 cmd[1] = "/C" ;
57 cmd[2] = args[0];
58 }
59 else if( osName.equals( "Windows 95" ) )
60 {
61 cmd[0] = "command.com" ;
62 cmd[1] = "/C" ;
63 cmd[2] = args[0];
64 }
65
66 Runtime rt = Runtime.getRuntime();
67 System.out.println("Execing " + cmd[0] + " " + cmd[1]
68 + " " + cmd[2]);
69 Process proc = rt.exec(cmd);
70 // any error message?
71 StreamGobbler errorGobbler = new
72 StreamGobbler(proc.getErrorStream(), "ERROR");
73
74 // any output?
75 StreamGobbler outputGobbler = new
76 StreamGobbler(proc.getInputStream(), "OUTPUT");
77
78 // kick them off
79 errorGobbler.start();
80 outputGobbler.start();
81
82 // any error???
83 int exitVal = proc.waitFor();
84 System.out.println("ExitValue: " + exitVal);
85
86 } catch (Throwable t)
87 {
88 t.printStackTrace();
89 }
90 }
91 }
Listing 1.4 (continued)
When Runtime.exec() Won't 11
Running GoodWindowsExec with the dir command produces:
E:\classes\org\javapitfalls\item1>java org.javapitfalls.item1.GoodWindowsExec "dir *.java"
Execing cmd.exe /C dir *.java OUTPUT> Volume in drive E has no label.
OUTPUT> Volume Serial Number is 5C5F-0CC9 OUTPUT>
OUTPUT> Directory of E:\classes\com\javaworld\jpitfalls\article2 OUTPUT>
OUTPUT>10/23/00 09:01p 805 BadExecBrowser.java
OUTPUT>10/22/00 09:35a 770 BadExecBrowser1.java
... (some output omitted for brevity)
OUTPUT>10/09/00 05:47p 23,543 TestStringReplace.java
OUTPUT>10/12/00 08:55p 228 TopLevel.java
OUTPUT> 22 File(s) 46,661 bytes
OUTPUT> 19,678,420,992 bytes free
ExitValue: 0
Running GoodWindowsExec with any associated document type will launch the application associated with that document type. For example, to launch Microsoft Word to display a Word document (a .doc extension), you type
>java org.javapitfalls.item1.GoodWindowsExec "yourdoc.doc" Notice that GoodWindowsExec uses the os.name system property to determine which Windows operating system you are running in order to use the appropriate command interpreter. After execing the command interpreter, we handle the standard error and standard input streams with the StreamGobbler class. The StreamGob-bler class empties any stream passed into it in a separate thread. The class uses a simple String type to denote which stream it is emptying when it prints the line just read to the console. So, the third pitfall to avoid is to know whether you are executing a standalone executable or an interpreted command. At the end of this section, I will demonstrate a simple command-line tool that will help you with that analysis.
It is important to note that the Process method used to get the output stream of the process is called getInputStream(). The thing to remember is that the perspective is from the Java program and not the external process. So, the output of the external program is the input to the Java program. And that logic carries over to the external programs input stream, which is an output stream to the Java program.
One final pitfall to cover with Runtime.exec() is not to assume that the exec() accepts any String that your command line (or shell) accepts. It is much more limited and not cross-platform. The primary cause of this pitfall is users attempting to use the exec() method that accepts a single String just like a command line. This confusion may be due to the parameter name for the exec() method being command. The
12 Item 1
programmer incorrectly associates the parameter command with anything he or she can type on a command line instead of associating it with a single program and its arguments, for example, a user trying to execute a program and redirect its output in one call to exec(). Listing 1.5 attempts to do just that.
Previous << 1 .. 4 5 6 7 8 9 < 10 > 11 12 13 14 15 16 .. 166 >> Next