1 |
filippis |
13 |
package tools; |
2 |
|
|
|
3 |
|
|
import java.io.*; |
4 |
duarte |
32 |
|
5 |
filippis |
39 |
/** |
6 |
|
|
* Package: tools |
7 |
|
|
* Class: PyMol |
8 |
|
|
* Author: Real Gagnon (http://www.rgagnon.com/javadetails/java-0014.html) |
9 |
|
|
* Copyright by Ioannis Filippis, filippis@molgen.mpg.de |
10 |
|
|
* Date: 03/03/2006 |
11 |
|
|
* |
12 |
|
|
* SystemCmd's static exec method will spawn an external process to execute an external |
13 |
|
|
* program or to launch a unix script or just execute a simple linux command. |
14 |
|
|
* The output is captured and returned as string. |
15 |
|
|
* |
16 |
|
|
* Notes: |
17 |
|
|
* - There are two versions of the exec method, one taking a String array and one just a String |
18 |
|
|
* - If you want to launch a Unix script and especially use the < > | piping options, |
19 |
|
|
* you must invoke the command processor |
20 |
|
|
* String[] cmd = {"/bin/sh", "-c", "ls > hello"}; |
21 |
|
|
* SystemCmd.exec(cmd); |
22 |
|
|
* - If you need to pass arguments, it's safer to a String array especially if they contain spaces. |
23 |
|
|
* String[] cmd = { "myProgram.exe", "-o=This is an option" }; |
24 |
|
|
* SystemCmd.exec(cmd); |
25 |
|
|
* |
26 |
|
|
* Changelog: |
27 |
|
|
* 14/03/06 Added exec method to take only a string as argument by JD |
28 |
|
|
* 03/03/06 first created/copied by IF |
29 |
|
|
*/ |
30 |
|
|
|
31 |
filippis |
13 |
public class SystemCmd { |
32 |
|
|
|
33 |
duarte |
32 |
public static String exec(String[] cmd) { |
34 |
|
|
String output = ""; |
35 |
|
|
try { |
36 |
|
|
String line = ""; |
37 |
|
|
Process p = Runtime.getRuntime().exec(cmd); |
38 |
|
|
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); |
39 |
|
|
while ((line = input.readLine()) != null) { |
40 |
|
|
output = output + line + "\n"; |
41 |
|
|
} |
42 |
|
|
input.close(); |
43 |
|
|
|
44 |
|
|
} catch (Exception err) { |
45 |
|
|
err.printStackTrace(); |
46 |
|
|
} |
47 |
|
|
return output; |
48 |
|
|
} |
49 |
filippis |
13 |
|
50 |
duarte |
32 |
public static String exec(String cmd) { |
51 |
|
|
String output = ""; |
52 |
|
|
try { |
53 |
|
|
String line = ""; |
54 |
|
|
Process p = Runtime.getRuntime().exec(cmd); |
55 |
|
|
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); |
56 |
|
|
while ((line = input.readLine()) != null) { |
57 |
|
|
output = output + line + "\n"; |
58 |
|
|
} |
59 |
|
|
input.close(); |
60 |
|
|
|
61 |
|
|
} catch (Exception err) { |
62 |
|
|
err.printStackTrace(); |
63 |
|
|
} |
64 |
|
|
return output; |
65 |
filippis |
13 |
} |
66 |
|
|
|
67 |
duarte |
32 |
|
68 |
|
|
} |