FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-code-example/ExecuteLine/ShellExecute2.java at master · cherkavi/java-code-example · GitHub
cherkavi
/
java-code-example
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
0
Code
Issues
0
Pull requests
42
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-code-example
/
ExecuteLine
/
ShellExecute2.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
109 lines (95 loc) · 3.35 KB
Breadcrumbs
java-code-example
/
ExecuteLine
/
ShellExecute2.java
Copy path
File metadata and controls
executable file
·
109 lines (95 loc) · 3.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
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
=
null
;
String
type
=
null
;
StringBuffer
output
=
null
;
StreamGobbler
(
InputStream
is
,
String
type
)
{
this
.
is
=
is
;
this
.
type
=
type
;
}
public
void
run
()
{
output
=
new
StringBuffer
();
try
{
InputStreamReader
isr
=
new
InputStreamReader
(
is
);
BufferedReader
br
=
new
BufferedReader
(
isr
);
String
line
=
null
;
while
( (
line
=
br
.
readLine
()) !=
null
){
output
.
append
(
line
);
// System.out.println(this.type+"> "+line);
}
}
catch
(
IOException
ioe
){
ioe
.
printStackTrace
();
}
}
public
String
getOutput
(){
return
this
.
output
.
toString
();
}
}
/**
* @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-");
}
/**
* @param command - Windows command
* @return
* <ul>
* <li><b>0</b> - âûïîëíåíî </li>
* <li><b>>0</b> - åñòü îøèáêè </li>
* </ul>
* */
public
static
String
executeWithOutput
(
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???
proc
.
waitFor
();
return
outputGobbler
.
getOutput
();
// System.out.println("ExitValue: " + exitVal);
// System.out.println("-end-");
}
}
Back
|
FazBrowse Home
|
New Git URL