2012-01-06 103 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,顺便说一句)。

相关问题