簡體   English   中英

通過Java訪問命令提示符(Windows)

[英]Accessing command prompt (windows) via java

我可以使用ProcessBuilder或Java庫中的其他工具來訪問Windows中的命令提示符並執行命令嗎? 我知道你可以在Mac上使用終端,我做到了:

ProcessBuilder pb = new ProcessBuilder(
      "bash",
      "-c",
      "cd " + System.getProperty("user.dir") + ";" + "someCommandInThatDirectory"
      );

Process p = pb.start();

要在某個目錄中執行某些命令,它們是否通過Windows中的命令提示符執行類似的操作?

我有這個檢查操作系統

String os = System.getProperty("os.name");
  if (os == "Mac OS X") {
    //do the mac thing
    }
  else if (os == "Windows XP" /*blah blah rest of windows types*/) {
    //do the windows one
    }

您可以使用Runtime.getRuntime()。exec:

String command = "cmd /c start cmd.exe ...your other commands";
try {
    Process process = Runtime.getRuntime().exec(command);
} catch (IOException e) {
    e.printStackTrace();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM