-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathShellExecute.java
executable file
·71 lines (60 loc) · 2.01 KB
/
ShellExecute.java
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
package com.cherkashin.vitaliy.application;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ShellExecute {
class StreamGobbler extends Thread
{
InputStream is;
String type;
StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}
public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
System.out.println(this.type+"> "+line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
/**
* @param command - Windows command
* @return
* <ul>
* <li><b>0</b> - âûïîëíåíî </li>
* <li><b>>0</b> - åñòü îøèáêè </li>
* </ul>
* */
public static int execute(String command) throws Exception {
// System.out.println("begin");
String[] cmd=new String[3];
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = command;
Runtime rt = Runtime.getRuntime();
// System.out.println("Execing " + cmd[0] + " " + cmd[1] + " " + cmd[2]);
Process proc = rt.exec(cmd);
// any error message?
// ShellExecute.StreamGobbler errorGobbler = new ShellExecute().new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
// ShellExecute.StreamGobbler outputGobbler = new ShellExecute().new StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
// errorGobbler.start();
// outputGobbler.start();
// any error???
return proc.waitFor();
// System.out.println("ExitValue: " + exitVal);
// System.out.println("-end-");
}
}