在Java中,我希望能夠執行Windows命令。如何使用Java執行Windows命令 - 更改網絡設置
有問題的命令是netsh
。這將使我能夠設置/重置我的IP地址。
請注意,我不想執行批處理文件。
而不是使用批處理文件,我想直接執行這些命令。這可能嗎?
這裏是我的未來參考實施的解決方案:
public class JavaRunCommand {
private static final String CMD =
"netsh int ip set address name = \"Local Area Connection\" source = static addr = 192.168.222.3 mask = 255.255.255.0";
public static void main(String args[]) {
try {
// Run "netsh" Windows command
Process process = Runtime.getRuntime().exec(CMD);
// Get input streams
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// Read command standard output
String s;
System.out.println("Standard output: ");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// Read command errors
System.out.println("Standard error: ");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}
這已經回答了很多次,只要看看計算器建議找一些他們的 – SJuan76
@ SJuan76,我道歉。你也許可以聯繫我的一些這些問題? – mre
@mre只要看看在側邊欄。 –