2012-01-06 165 views
0

我已經使用install4j創建了一個windows服務,一切正常,但現在我需要將它傳遞給服務的命令行參數。我知道我可以在服務創建的時間在新的服務嚮導來配置他們,但我希望無論是傳遞參數給註冊服務命令,即:install4j:我如何將命令行參數傳遞給windows服務

myservice.exe --install --arg arg1=val1 --arg arg1=val2 "My Service Name1" 

或者讓他們在.vmoptions文件,如:

-Xmx256m 
arg1=val1 
arg2=val2 

好像要做到這一點的唯一方法就是修改我的代碼通過exe4j.launchName拿起服務名稱,然後加載有特定服務的必要配置一些其他的文件或環境變量。我過去曾使用過其他的Java服務創建工具,並且都直接支持用戶註冊的命令行參數。

回答

0

我知道你在一月問過這個問題,但你有沒有想過這個?

我不知道你從哪裏採購val1,val2等。它們是否由用戶輸入到安裝過程中的表單中的字段中?假設他們是,那麼這是一個類似的問題,我遇到了一段時間。

我的方法是使用一個帶有必要字段(作爲文本字段對象)的可配置表單,並且顯然具有分配給文本字段值的變量(在「用戶輸入/變量名稱」類別下文本域)。

在安裝過程的後面,我有一個顯示進度屏幕,並附帶一個運行腳本動作,並附帶一些java來實現我想要做的事情。

這樣可以在install4j中選擇設置變量時有兩個'陷阱'。首先,變量HAS無論如何設置,即使它只是空字符串。因此,如果用戶將字段留空(即他們不想將該參數傳遞到服務中),則仍然需要爲「運行」可執行文件或「啓動服務」任務提供一個空字符串(更多內容在一瞬間)其次,參數不能有空格 - 每個空格分隔的參數都必須有自己的行。

考慮到這一點,這裏有一個運行腳本代碼片段可能實現你想要的:

final String[] argumentNames = {"arg1", "arg2", "arg3"}; 
// For each argument this method creates two variables. For example for arg1 it creates 
// arg1ArgumentIdentifierOptional and arg1ArgumentAssignmentOptional. 
// If the value of the variable set from the previous form (in this case, arg1) is not empty, then it will 
// set 'arg1ArgumentIdentifierOptional' to '--arg', and 'arg1ArgumentAssignmentOptional' to the string arg1=val1 (where val1 
// was the value the user entered in the form for the variable). 
// Otherwise, both arg1ArgumentIdentifierOptional and arg1ArgumentAssignmentOptional will be set to empty. 
// 
// This allows the installer to pass both parameters in a later Run executable task without worrying about if they're 
// set or not. 

for (String argumentName : argumentNames) { 
    String argumentValue = context.getVariable(argumentName)==null?null:context.getVariable(argumentName)+""; 
    boolean valueNonEmpty = (argumentValue != null && argumentValue.length() > 0); 
    context.setVariable(
     argumentName + "ArgumentIdentifierOptional", 
     valueNonEmpty ? "--arg": "" 
    ); 
    context.setVariable(
     argumentName + "ArgumentAssignmentOptional", 
     valueNonEmpty ? argumentName+"="+argumentValue : "" 
    );  
} 

return true; 

的最後一步是啓動服務或可執行文件。我不太確定服務是如何工作的,但通過可執行文件,您可以創建任務,然後編輯「參數」字段,併爲其提供行分隔的值列表。

所以你的情況,這可能是這樣的:

--install 
${installer:arg1ArgumentIdentifierOptional} 
${installer:arg1ArgumentAssignmentOptional} 
${installer:arg2ArgumentIdentifierOptional} 
${installer:arg2ArgumentAssignmentOptional} 
${installer:arg3ArgumentIdentifierOptional} 
${installer:arg3ArgumentAssignmentOptional} 

「我的服務名稱1」

就是這樣。如果其他人知道如何做到這一點,可以隨意改善這種方法(這是爲install4j 4.2.8,順便說一句)。

相關問題