在注册表中的环境中添加多个值并在Java应用程序中进行检索 [英] Adding multiple values to an environment in registry and retrieving in Java application
问题描述
我正在使用procrun将Java应用程序作为服务运行.
I am using procrun to run a java application as a service.
我使用以下方法设置路径:
I used following to set path:
set PATH="dir1;dir2;%PATH%"
procrun.exe //US//%SERVICE_NAME% ++Environment PATH=%PATH%
将注册表中的环境值更新为:
which updates environment value in registry as:
但是,在我的Java应用程序中,当我尝试获取 PATH 的值时:
But, in my Java application when I try to get the value of PATH:
System.getenv("PATH")
我仅获得第一个值(即上述情况下的dir1.如果将路径设置为 dir2; dir1;%PATH%
,则仅获得dir2)
I get only the first value ( i.e. dir1 in above case. If I set the path to dir2;dir1;%PATH%
, I get only dir2)
我是通过错误的方式检索还是以错误的方式进行设置?
Am I retrieving in a wrong way or setting in a wrong way?
推荐答案
您的问题是由于-Environment
选项中缺少引号引起的:
Your problem is caused by the lack of quoting in the --Environment
option:
将以key = value形式提供给服务的环境变量列表.它们用#或;分隔.人物.如果您需要嵌入#或;值中的字符将它们放在单引号内.
List of environment variables that will be provided to the service in the form key=value. They are separated using either # or ; characters. If you need to embed either # or ; character within a value put them inside single quotes.
(请参见 Procrun文档).
由于Windows将%PATH%
扩展为;
分隔的路径的列表,因此需要使用:
Since Windows expands %PATH%
to a list of ;
-separated paths, you need to use:
procrun.exe //US//%SERVICE_NAME% ++Environment 'PATH=%PATH%'
或 PATH ='%PATH'
(如果愿意).将单引号字符分成;
分隔的参数列表后,将其从参数字符串中剥离(请参见
or PATH='%PATH'
if you prefer. The single quote character is stripped from the parameter string after breaking it into a ;
-separated list of parameters (cf. source code). Hence there might be a problem if you have single quotes in the name of a folder.
编辑:procrun还接受双引号(未去除)的引用,并且您的 PATH
变量包含引号,但cmd.exe的魔法将其删除.
Edit: procrun also accepts quoting through double quotes (which are not stripped) and your PATH
variable contains quotes, but the magic of cmd.exe strips them away.
这篇关于在注册表中的环境中添加多个值并在Java应用程序中进行检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!