Skip to content

Instantly share code, notes, and snippets.

@crazylion
Created May 14, 2015 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazylion/44b8ad7d5e04b3b88847 to your computer and use it in GitHub Desktop.
Save crazylion/44b8ad7d5e04b3b88847 to your computer and use it in GitHub Desktop.
run cmd with exception
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