Java中如何插入多个OS命令行?
In Java, how to insert multiple OS commands line?
我使用以下代码在 Windows 上执行简单的 OS 命令:
public class Ping {
public static void main(String[] args) throws IOException {
String command = "ping google.com";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
System.out.println();
System.out.println("Finished");
如何修改代码以插入多个命令而不是一个,假设我想 ping google.com,然后再 ping yahoo.com。
我尝试创建如下数组字符串:
String [] command = {"ping google.com", "ping yahoo.com"};
但是,这显示了一个错误。
感谢您对此提供的帮助。
使用循环:
String [] commands = {"ping google.com", "ping yahoo.com"};
for(String command: commands) {
Process process = Runtime.getRuntime().exec(command);
//more stuff
}
我使用以下代码在 Windows 上执行简单的 OS 命令:
public class Ping {
public static void main(String[] args) throws IOException {
String command = "ping google.com";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
System.out.println();
System.out.println("Finished");
如何修改代码以插入多个命令而不是一个,假设我想 ping google.com,然后再 ping yahoo.com。 我尝试创建如下数组字符串:
String [] command = {"ping google.com", "ping yahoo.com"};
但是,这显示了一个错误。
感谢您对此提供的帮助。
使用循环:
String [] commands = {"ping google.com", "ping yahoo.com"};
for(String command: commands) {
Process process = Runtime.getRuntime().exec(command);
//more stuff
}