从Java打开一个新的提示/terminal窗口

我想打开一个新的terminal窗口,打开后会运行一个命令。 它最好是一个真正的本地窗口,我不介意为linux / osx / windows编写不同的代码。

我假设一个模拟terminal将工作,只要它支持一个真正的terminal将做的一切,而不仅仅是打印命令的输出线。

打开实际的terminal窗口肯定会为每个操作系统需要不同的代码。 对于Mac,你需要像这样的东西:

Runtime.getRuntime().exec("/usr/bin/open -a Terminal /path/to/the/executable"); 

这会工作吗?

 // windows only Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe"); p.waitFor(); 

您需要关于您正在运行的操作系统的信息。 为此,你可以使用这样的代码:

 public static void main(String[] args) { String nameOS = "os.name"; String versionOS = "os.version"; String architectureOS = "os.arch"; System.out.println("\n The information about OS"); System.out.println("\nName of the OS: " + System.getProperty(nameOS)); System.out.println("Version of the OS: " + System.getProperty(versionOS)); System.out.println("Architecture of THe OS: " + System.getProperty(architectureOS)); } 

那么对于每个操作系统,您将不得不使用Bala R和Mike Baranczak描述的不同的调用

我已经在Ubuntu(X11 Desktop)10.04〜14.04以及其他Debian发行版上使用过。 工作正常; 虽然,你可以考虑使用Java的ProcessBuilder

      // GNU / Linux  - 例子

 runtime.getRuntime()。exec(“/ usr / bin / x-terminal-emulator --disable-factory -e cat README.txt”);

  //  - 禁用工厂不要注册激活名称服务器,不要重新使用活动terminal
 // -e在terminal内执行此选项的参数。