2011-04-21 153 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