source: other-projects/FileTransfer-WebSocketPair/testGXTWithGreenstone/src/org/greenstone/gatherer/util/ExternalProgram.java@ 33053

Last change on this file since 33053 was 33053, checked in by ak19, 5 years ago

I still had some stuff of Nathan Kelly's (FileTransfer-WebSocketPair) sitting on my USB. Had already commited the Themes folder at the time, 2 years back. Not sure if he wanted this additional folder commited. But I didn't want to delete it and decided it will be better off on SVN. When we use his project, if we find we didn't need this test folder, we can remove it from svn then.

File size: 2.7 KB
Line 
1package org.greenstone.gatherer.util;
2
3
4import java.io.BufferedReader;
5import java.io.BufferedWriter;
6import java.io.InputStreamReader;
7import java.io.IOException;
8import java.io.OutputStreamWriter;
9import org.greenstone.gatherer.DebugStream;
10
11
12/**
13 * This class provides a convenient method of running external programs and viewing/analysing
14 * their output. The external programs may be run with or without arguments.
15 */
16public class ExternalProgram
17{
18 private Process programProcess = null;
19
20 private BufferedReader programOut = null;
21 private BufferedReader programErr = null;
22
23
24 /**
25 * Prepares to run a specified external program with the specified arguments.
26 *
27 * @param programName The name (and path, if necessary) of the program to run.
28 * @param programArgs The arguments to provide to the program when run.
29 */
30 public ExternalProgram(String programName, String programArgs)
31 throws IOException
32 {
33 // Run the program
34 programProcess = Runtime.getRuntime().exec(programName + " " + programArgs);
35
36 // Grab the program output
37 programOut = new BufferedReader(new InputStreamReader(programProcess.getInputStream()));
38 programErr = new BufferedReader(new InputStreamReader(programProcess.getErrorStream()));
39
40 }
41
42
43
44 /**
45 */
46 public int exitProgram()
47 throws IOException
48 {
49 // Finished, thank you
50 //programIn.close();
51
52 // Wait for the process to finish
53 try {
54 programProcess.waitFor();
55 }
56 catch (InterruptedException ex) {
57 throw new Error("Internal Error: Process interrupted.\n" + ex);
58 }
59
60 // Return the program exit value
61 return programProcess.exitValue();
62 }
63
64
65 /**
66 * Returns a single line of output sent by the program to standard output.
67 *
68 * @return a single line of output sent by the program to standard output.
69 */
70 public String getLineOfProgramOutput()
71 {
72 // Read in a line from the program output
73 try {
74 if (programOut.ready())
75 return programOut.readLine();
76 else
77 return null;
78 }
79 catch (IOException ex) {
80 DebugStream.println("Error: Exception occurred while reading program output.");
81 DebugStream.println("Exception: " + ex);
82 return null;
83 }
84 }
85
86
87 /**
88 * Returns a single line of output sent by the program to standard error.
89 *
90 * @return a single line of output sent by the program to standard error.
91 */
92 public String getLineOfProgramError()
93 {
94 // Read in a line from the program error
95 try {
96 if (programErr.ready())
97 return programErr.readLine();
98 else
99 return null;
100 }
101 catch (IOException ex) {
102 DebugStream.println("Error: Exception occurred while reading program error.");
103 DebugStream.println("Exception: " + ex);
104 return null;
105 }
106 }
107}
Note: See TracBrowser for help on using the repository browser.