One: Window under the execution of CMD specified
Program Examples:
/* This method implements the automatic file copy function. Copy the specified file name from the source path to the destination path * Using system commands automatically create a destination path */public static Boolean CopyFile if the destination path does not exist
(String Origpath, String destpath, String filename) throws exception{String osname = System.getproperty ("Os.name");
Boolean flag = false; /* System command-supported operating system Windows XP, 2003 7*/if (!) ( Osname.equalsignorecase ("Windows XP") | | Osname.equalsignorecase ("Windows 2000") | | Osname.equalsignorecase ("Windows 2003") | |
Osname.equalsignorecase ("Windows 7")) {return flag;
Runtime RT = Runtime.getruntime ();
Process p = null;
File F = new file (destpath);
if (!f.exists ()) {f.mkdirs ();
int exitval;
p = rt.exec ("cmd exe/c copy" + origpath+filename+ "" +destpath); The export value of the process.
By convention, 0 indicates normal termination.
Exitval = P.waitfor ();
if (Exitval = = 0) {flag = true;
}else{flag = false;
return flag; } public static void Main (string[] args) { try { CopyFile ("
D:\\data\\ "," d:\\a\\ "," 131204.txt "); catch (Exception e) { //TODO auto-generated catch block
E.printstacktrace (); }
}
Second: Execute shell command under Linux
Program Examples:
Package edu.test;
Import Java.io.InputStreamReader;
Import Java.io.LineNumberReader;
/** * Java executes Linux commands in a Linux environment and returns the command return value.
* @author Lee */public class Execlinuxcmd {public static Object exec (String cmd) {try {
String[] Cmda = {"/bin/sh", "-C", CMD};
Process process = Runtime.getruntime (). exec (CMDA);
LineNumberReader br = new LineNumberReader (New InputStreamReader (Process.getinputstream ()));
StringBuffer sb = new StringBuffer ();
String Line;
while (line = Br.readline ())!= null) {System.out.println (line);
Sb.append (line). append ("\ n");
return sb.tostring ();
catch (Exception e) {e.printstacktrace ();
return null; public static void Main (string[] args) {//TODO auto-generated method stub String pwdstring = EXEC ("pwd"). ToString ();
String netsstring = exec ("netstat-nat|grep-i \" 80\ "|wc-l"). ToString ();
System.out.println ("========== obtained value =============");
System.out.println (pwdstring);
System.out.println (netsstring); }
}
Execution results: