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


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

  • 1
    Where has this answer been all my life?! I wish I could give it a million upvotes. Thank you! Mar 21 at 21:01

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:
User variable:

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.

  • 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.
    – user47078
    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 !
    – Chang Zhao
    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. 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
    – VertigoRay
    Jun 12, 2022 at 2:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .