Java执行cmd命令并接收返回结果

时间:2022-09-03 16:56:10
	String command="ipconfig -all";
		String s="IPv4";
		String line = null;
		StringBuilder sb = new StringBuilder();
		Runtime runtime = Runtime.getRuntime();
		try {
		Process process = runtime.exec(command);
		BufferedReader	bufferedReader = new BufferedReader
				(new InputStreamReader(process.getInputStream()));
		
		
			while ((line = bufferedReader.readLine()) != null) {
				sb.append(line + "\n");
				if (line.contains(s)) {
					System.out.println(line);
				}
			}
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		//System.out.println(sb.toString());

Java执行cmd命令并接收返回结果