2011-08-02 52 views
-2

目前我正在做一個項目,我需要將參數從Java應用程序(基本上是Java Swing)傳遞到Cygwin命令界面。我怎樣才能做到這一點?如何將Java應用程序中的參數傳遞給Cygwin命令?

感謝

+2

你打算怎麼打cygwin?這裏稍微詳細一點會很有用。 – 2011-08-02 14:37:19

+0

是的,你是對的!我應該添加一些細節,我用java ..這是我使用過的「Runtime.getRuntime()。exec(」cmd/c start C:\\ cygwin \\ cygwin.bat「);」我有我的cygwin.bat在上面的路徑。 – Anis

回答

2

如果你看看Cygwin.bat的內容,你會看到它調用bash.exe二進制:

@echo off 

C: 
chdir C:\cygwin\bin 

bash --login -i 

命令二進制文件通常有一個help說法。在這種情況下,bash的肯定作用:

bash --help 
GNU bash, version 3.2.49(23)-release-(i686-pc-cygwin) 
Usage: bash [GNU long option] [option] ... 
     bash [GNU long option] [option] script-file ... 
GNU long options: 
     --debug 
     --debugger 
     --dump-po-strings 
     --dump-strings 
     --help 
     --init-file 
     --login 
     --noediting 
     --noprofile 
     --norc 
     --posix 
     --protected 
     --rcfile 
     --restricted 
     --verbose 
     --version 
     --wordexp 
Shell options: 
     -irsD or -c command or -O shopt_option   (invocation only) 
     -abefhkmnptuvxBCHP or -o option 
Type `bash -c "help set"' for more information about shell options. 
Type `bash -c help' for more information about shell builtin commands. 
Use the `bashbug' command to report bugs. 

現在我們知道哪些選項需要直接,Java應用程序可以調用的bash:

String commandString = "help"; 
Runtime rt = Runtime.getRuntime(); 
Process pr = rt.exec("C:\\cygwin\\bin\\bash -c " + commandString); 

記住與你的揮杆的值來代替commandString零件。

+0

請你詳細說明這個代碼「Process pr = rt.exec(」cygwin-command「+ swingValue); 」例如我想在cygwin命令行界面中找到「幫助」,所以我必須輸入「help」在cygwin命令提示符中,我怎麼能通過將「help」值通過java gui接口傳遞給cygwin命令提示符來實現。謝謝 – Anis

+0

我還不完全清楚你在做什麼。我已經重寫了我的答案,以引導您將命令推送到cygwin。你必須自己弄清具體細節,或者提供更多細節,如果我再次失去目標。 – jkeeler

相關問題