Created
May 14, 2015 00:33
-
-
Save crazylion/44b8ad7d5e04b3b88847 to your computer and use it in GitHub Desktop.
run cmd with exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
boolean _isWin=false; | |
void runCmd(String cmd) { | |
Process p; | |
InputStream stderr=null; | |
InputStreamReader esr=null ; | |
BufferedReader ebr=null; | |
InputStream stdout=null; | |
InputStreamReader osr=null; | |
BufferedReader obr =null; | |
try { | |
if (_isWin) { | |
p = Runtime.getRuntime().exec(new String[] { | |
"cmd.exe", "/c", cmd | |
} | |
); | |
} else { | |
p = Runtime.getRuntime().exec(new String[] { | |
"bash", "-c", cmd | |
} | |
); | |
} | |
String line=null; | |
stderr = p.getErrorStream (); | |
esr = new InputStreamReader (stderr); | |
ebr = new BufferedReader (esr); | |
System.out.println ("<error>"); | |
while ( (line = ebr.readLine ()) != null) | |
System.out.println(line); | |
System.out.println ("</error>"); | |
stdout = p.getInputStream (); | |
osr = new InputStreamReader (stdout); | |
obr = new BufferedReader (osr); | |
System.out.println ("<output>"); | |
while ( (line = obr.readLine ()) != null) | |
System.out.println(line); | |
System.out.println ("</output>"); | |
int val =p.waitFor(); | |
println("run val="+val); | |
} | |
catch(Exception e) { | |
print("convert exception"); | |
print(e); | |
} | |
finally { | |
try { | |
ebr.close(); | |
esr.close(); | |
stderr.close(); | |
obr.close(); | |
osr.close(); | |
stdout.close(); | |
} | |
catch(Exception e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment