//run the operating system command, and then sends the output to the console,
package IO;
import java.io.*;
Public class OSExecute {
Public static void command (String command) {
Boolean err=false;
Try {
//according to the incoming command sequence, to create a process
Process Process=new ProcessBuilder (command. The split (" ")). The start ();
//get the process output stream
BufferedReader results=new BufferedReader (
New InputStreamReader (process. GetInputStream ()));
String s;
While ((s=the readLine ())!=null)
System.out.println(s);
//get the process error stream
BufferedReader errors=new BufferedReader (
New InputStreamReader (process. GetErrorStream (), "GBK"));
//if there is a problem, report the error and will be a non-zero value is returned to the calling process:
While ((s=errors. ReadLine ())!=null) {
System. Err. Println (s);
err=true;
}
} the catch (Exception e) {
//if it is a Windows 2000, it throws the default command line abnormality:
if(! Command. The startsWith (" CMD/C "))
The command (+ command "CMD/C");
The else
Throw new RuntimeException (e);
}
//if the process itself an error in the process of execution, a runtime exception is thrown separate custom
If (err)
Throw new OSExecuteException (" Errors executing "+ command).
}
}
Package IO;
Public class OSExecuteDemo {
Public static void main (String [] args) {
//decompiled OSExecuteDemo
OSExecute.com mand (" javap/bin/IO/OSExecuteDemo ");
}
}
Run the program, the results are as follows:
Compiled from "OSExecuteDemo. Java
"Public class IO. OSExecuteDemo {
Public IO. OSExecuteDemo ();
Public static void main (Java. Lang. String []);
}
warning: binary files/bin/IO/OSExecuteDemo contains IO OSExecuteDemo
The Exception in the thread "main" IO. OSExecuteException: Errors executing javap/bin/IO/OSExecuteDemo
Mand at io.OSExecute.com (OSExecute. Java: 34)
The at IO. OSExecuteDemo. Main (OSExecuteDemo. Java: 6)
Decompiling command execution is normal, but always throw an exception, what reason is this excuse me? How to solve?