install4j:如何将命令行参数传递给Windows服务

发布于 2024-12-25 10:02:33 字数 421 浏览 1 评论 0原文

我已经使用 install4j 创建了一个 Windows 服务,一切正常,但现在我需要将其命令行参数传递给该服务。我知道我可以在新服务向导中的服务创建时配置它们,但我希望将参数传递给注册服务命令,即:

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

或者将它们放入 .vmoptions 文件中,例如:

-Xmx256m
arg1=val1
arg2=val2

这似乎是唯一的方法这是修改我的代码以通过 exe4j.launchName 获取服务名称,然后加载具有该特定服务所需配置的其他文件或环境变量。我过去使用过其他 Java 服务创建工具,它们都对用户注册的命令行参数提供直接支持。

I've created a windows service using install4j and everything works but now I need to pass it command line arguments to the service. I know I can configure them at service creation time in the new service wizard but i was hoping to either pass the arguments to the register service command ie:

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

or by putting them in the .vmoptions file like:

-Xmx256m
arg1=val1
arg2=val2

It seems like the only way to do this is to modify my code to pick up the service name via exe4j.launchName and then load some other file or environment variables that has the necessary configuration for that particular service. I've used other service creation tools for java in the past and they all had straightforward support for command line arguments registered by the user.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情换悲伤 2025-01-01 10:02:33

我知道你在一月份就问过这个问题,但你现在明白了吗?

我不知道你从哪里获取 val1、val2 等。它们是否由用户在安装过程中输入到表单的字段中?假设它们是,那么这与我不久前遇到的问题类似。

我的方法是拥有一个带有必要字段(作为文本字段对象)的可配置表单,并且显然将变量分配给文本字段的值(在文本字段的“用户输入/变量名称”类别下)。

后来在安装过程中,我有一个显示进度屏幕,其中附有一个运行脚本操作,并带有一些 java 来实现我想做的事情。

当以这种方式在 install4j 中选择设置变量时,有两个“陷阱”。首先,无论如何都必须设置变量,即使它只是空字符串。因此,如果用户将该字段留空(即他们不想将该参数传递到服务中),您仍然需要向“运行可执行文件”或“启动服务”任务提供一个空字符串(稍后会详细介绍) ) 其次,参数不能有空格 - 每个以空格分隔的参数都必须有自己的行。

考虑到这一点,下面的运行脚本代码片段可能会实现您想要的效果:

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}

“My Service Name1”

就是这样。如果其他人知道如何更好地做到这一点,请随意改进此方法(顺便说一句,这是针对 install4j 4.2.8 的)。

I know you asked this back in January, but did you ever figure this out?

I don't know where you're sourcing val1, val2 etc from. Are they entered by the user into fields in a form in the installation process? Assuming they are, then this is a similar problem to one I faced a while back.

My approach for this was to have a Configurable Form with the necessary fields (as Text Field objects), and obviously have variables assigned to the values of the text fields (under the 'User Input/Variable Name' category of the text field).

Later in the installation process I had a Display Progress screen with a Run Script action attached to it with some java to achieve what I wanted to do.

There are two 'gotchas' when optionally setting variables in install4j this way. Firstly, the variable HAS to be set no matter what, even if it's just to the empty string. So, if the user leaves a field blank (ie they don't want to pass that argument into the service), you'll still need to provide an empty string to the Run executable or Launch Service task (more in that in a moment) Secondly, arguments can't have spaces - every space-separated argument has to have its own line.

With that in mind, here's a Run script code snippet that might achieve what you want:

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;

The final step is to launch the service or executable. I'm not too sure how services work, but with the executable, you create the task then edit the 'Arguments' field, giving it a line-separated list of values.

So in your case, it might look like this:

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

"My Service Name1"

And that's it. If anyone else knows how to do this better feel free to improve on this method (this is for install4j 4.2.8, btw).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文