String command = "cmd.exe /c ipconfig /all";//这里的/c 是什么意思 Process p = Runtime.getRuntime().exec(command);
可以用socket把执行的Dos命令传到另一台计算机,然后接受到之后执行命令
thinking in java 第四版551页//: net/mindview/util/OSExecute.java // Run an operating system command // and send the output to the console. package net.mindview.util; import java.io.*;public class OSExecute { public static void command(String command) { boolean err = false; try { Process process = new ProcessBuilder(command.split(" ")).start(); BufferedReader results = new BufferedReader( new InputStreamReader(process.getInputStream())); String s; while((s = results.readLine())!= null) System.out.println(s); BufferedReader errors = new BufferedReader( new InputStreamReader(process.getErrorStream())); // Report errors and return nonzero value // to calling process if there are problems: while((s = errors.readLine())!= null) { System.err.println(s); err = true; } } catch(Exception e) { // Compensate for Windows 2000, which throws an // exception for the default command line: if(!command.startsWith("CMD /C")) command("CMD /C " + command); else throw new RuntimeException(e); } if(err) throw new OSExecuteException("Errors executing " + command); } } ///:~ //: net/mindview/util/OSExecuteException.java package net.mindview.util;public class OSExecuteException extends RuntimeException { public OSExecuteException(String why) { super(why); } } ///:~
import java.io.IOException;
import java.io.InputStreamReader;public class DOSCommand {
public DOSCommand() {
} public static String getMACAddress() { String address = "";
String os = System.getProperty("os.name");
if (os != null && os.startsWith("Windows")) {
try {
String command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
if (line.indexOf("Physical Address") > 0) {
int index = line.indexOf(":");
index += 2;
address = line.substring(index);
break;
}
}
br.close();
return address.trim();
} catch (IOException e) {
e.printStackTrace();
}
}
return address;
} public static void main(String[] args) {
System.out.println(""+DOSCommand.getMACAddress());
}
}
java version "1.6.0_04"
Java(TM) SE Runtime Environment (build 1.6.0_04-b12)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
SOCKAT编程
不会啊 !
String str="net stop ServerMgr\n net start ServerMgr>E:\\1.txt";
WriteFile(str);
try {
String command = "cmd.exe /c"+"start D:\\sql2000\\SETUP.BAT";
Process child = Runtime.getRuntime().exec(command);
} catch (IOException e)
{
System.out.println("文件错误");
}
不过这不就是unix操作系统的原理么?
// Run an operating system command
// and send the output to the console.
package net.mindview.util;
import java.io.*;public class OSExecute {
public static void command(String command) {
boolean err = false;
try {
Process process =
new ProcessBuilder(command.split(" ")).start();
BufferedReader results = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String s;
while((s = results.readLine())!= null)
System.out.println(s);
BufferedReader errors = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
// Report errors and return nonzero value
// to calling process if there are problems:
while((s = errors.readLine())!= null) {
System.err.println(s);
err = true;
}
} catch(Exception e) {
// Compensate for Windows 2000, which throws an
// exception for the default command line:
if(!command.startsWith("CMD /C"))
command("CMD /C " + command);
else
throw new RuntimeException(e);
}
if(err)
throw new OSExecuteException("Errors executing " +
command);
}
} ///:~
//: net/mindview/util/OSExecuteException.java
package net.mindview.util;public class OSExecuteException extends RuntimeException {
public OSExecuteException(String why) { super(why); }
} ///:~