幫幫我 我是 Java 新手,目前正在做一個學校項目,需要我使用運行時來使用 netsh 來查找 WLAN ssid 和 mac 地址。但是控制臺一直給我這個(Microsoft Windows [Version 10.0.17134.165] (c) 2018 Microsoft Corporation。保留所有權利。)我如何讓它顯示 netsh 命令輸出public static void main(String args[]) {}public void getWLANbssidInfo() { String netsh = "netsh wlan show networks mode = bssid"; try { Process p1; p1 = Runtime.getRuntime().exec("cmd /c " + netsh); p1.waitFor(); BufferedReader reader = new BufferedReader(new InputStreamReader(p1.getInputStream())); String line = reader.readLine(); while(line!=null){ System.out.println(line); line = reader.readLine(); } } catch (IOException ex) { System.out.println("There was an IO exception."); } catch (InterruptedException ex) { System.out.println("The command was interrupted."); }}}
2 回答

桃花長相依
TA貢獻1860條經驗 獲得超8個贊
您必須使用exec()
接受 a的重載String[]
來指定命令和命令的參數,例如:
p1 = Runtime.getRuntime().exec(new String[]{"cmd", "/c", "netsh"});
請注意,netsh
如果可執行文件包含在運行命令的環境路徑或工作目錄(您可以設置)中,您也可以在沒有 cmd 命令的情況下運行該命令:
p1 = Runtime.getRuntime().exec("netsh");
添加回答
舉報
0/150
提交
取消