在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只要看看在侧边栏。 –