2011-04-21 157 views
7

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

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

回答

9

打开一个实际的终端窗口肯定需要为每个操作系统使用不同的代码。对于Mac,您需要类似的东西:

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

-a做什么? – clankill3r 2015-03-09 19:49:41

12

这项工作?

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

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

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)); 
    } 

然后为每个操作系统你将不得不使用不同的调用由

4

我用这个在Ubuntu巴拉R和麦克Baranczak描述(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. 
+0

- 禁用工厂在我的Peppermint Linux 7中不起作用 – 2017-06-30 20:47:52