16

I am running tomcat on a windows 2012 machine.

I need to set an environment variable before the service starts such that I can query the variable in Java in the web application like this:

String myVar = System.getenv("MY_VAR"); 

This variable is NOT a tomcat variable. It is a variable specific to our application.

I am not including tomcat in the tag for this question because this applies to any service; however, I would be happy with a tomcat specific answer.

Here is the question where I asked for a tomcat specific solution: https://superuser.com/questions/1142406/setting-user-environment-variables-for-tomcat-on-windows

I am using the stand way that Windows manages services that is accessed from the "Administrative Tools" -> "Services" to start and stop tomcat. So AFAIK, I don't have the usual control I have where I could put the environment variable in a .bat file that also starts the server process.

2 Answers 2

27

If you want to set an environment variable just for the service (regardless of what user it's running as) you can do so in the registry: HKLM\SYSTEM\CurrentControlSet\Services.

Here's a quick example of using regedit to add two environment variables to the Windows Update service (just for demonstration purposes, these aren't affecting the service):

  • FOO=Bar
  • KEY=SGVsbG8gV29ybGQhIQ==

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\wuauserv Environment REG_MULTI_SZ

Start (or restart, if it's already running) the service. We can then use Process Explorer to see that the environment variables are available:

C:\WINDOWS\system32\svchost.exe -k netsvcs -p -s wuauserv

3
  • 2
    Where has this answer been all my life?! I wish I could give it a million upvotes. Thank you! Commented Mar 21, 2023 at 21:01
  • @splatteredbits! 100% agreed. This deserves many, many up-votes. This has significant implications for services hardening and optimizaiton around things like the temp folder. Commented Jan 23 at 4:47
  • Just a couple of tips on this VERY helpful answer: 1) note that when creating that Environment element in regedit, you MUST use New>Multi-Size value (as indicated by that reg_multi_sz type in the screenshot) and NOT New>String Value (which would create a reg_sz type), otherwise starting the service will report simply "Parameter is Incorrect". And 2) note that sadly we can't use parameter substitution with this trick (as it's not a shell), so using PATH=yourfolder;%PATH% won't work. It would put that literal string %PATH% (after yourfolder) into the PATH env var for the service. Commented Feb 13 at 18:29
3

If it is a local Server 2012 environment variable you seek, you can use PowerShell to create a new system or user environment variable make sure you run PowerShell as Administrator.

Machine variable:
[Environment]::SetEnvironmentVariable('Name','Value','Machine')
User variable:
[Environment]::SetEnvironmentVariable('Name','Value','User')

To check the current environment variables, use the following PowerShell command

Get-Childitem ENV: 

Note: you will need to close PowerShell and open a new instance to see the newly created environment variable.

4
  • You only need to restart if you want to use the ENV provider. Set/Create your variable and then just use [Environment]::GetEnvironmentVariable() and it will return it with no restart. Commented Nov 7, 2016 at 3:16
  • Do we have to reboot windows ? I set in the PowerShell then opened new command prompt window and I can see the environment variable I set, but my service is not picking it up ! Commented Jan 8, 2022 at 16:20
  • Hi @ChangZhao, you shouldn't need to reboot, The most likely reason the service account cannot see the environment variable is you are running it under a different user account/context. Commented Jan 9, 2022 at 21:53
  • @ChangZhao, check out my answer and see if it's more what you were looking for: serverfault.com/a/1103091/145894 Commented Jun 12, 2022 at 2:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.