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

Mat*_*att 11 java terminal

我想打开一个新的终端窗口,它将在打开时运行某个命令.它最好需要是一个真正的本机窗口,我不介意为linux/osx/windows编写不同的代码.

我假设模拟终端可以工作,只要它支持真正的终端所做的一切,而不仅仅是从命令打印输出行.

Bal*_*a R 15

这会有用吗?

// windows only
Process p = Runtime.getRuntime().exec("cmd /c start cmd.exe");
p.waitFor();
Run Code Online (Sandbox Code Playgroud)


Mik*_*zak 11

打开一个实际的终端窗口肯定需要为每个OS提供不同的代码.对于Mac,您需要以下内容:

Runtime.getRuntime().exec("/usr/bin/open -a Terminal /path/to/the/executable");
Run Code Online (Sandbox Code Playgroud)

  • -a做什么? (6认同)

Jim*_*ios 6

在Ubuntu(X11桌面)10.04~14.04和其他Debian发行版上使用了这个.工作良好; 不过,您可以考虑使用Java的ProcessBuilder.

     // GNU/Linux -- example

Runtime.getRuntime().exec("/usr/bin/x-terminal-emulator --disable-factory -e cat README.txt");

 //  --disable-factory    Do not register with the activation nameserver, do not re-use an active terminal
//    -e                  Execute the argument to this option inside the terminal.