2

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:

enter image description here

But, in my Java application when I try to get the value of PATH:

System.getenv("PATH")

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?

tryingToLearn
  • 10,691
  • 12
  • 80
  • 114

1 Answers1

0

Your problem is caused by the lack of quoting in the --Environment option:

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.

(cf. Procrun documentation).

Since Windows expands %PATH% to a list of ;-separated paths, you need to use:

procrun.exe //US//%SERVICE_NAME% ++Environment '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.

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.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43