title: Setting User Environment Variables for tomcat on Windows
body: <p>I am running tomcat 8 on windows 2012.</p>
  
  <p>I have a webapp that needs an user environment variable to be set in order to run.</p>
  
  <p>I can start tomcat and retrieve the variable successfully like this:</p>
  
  <pre><code>set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_65
  set CATALINA_HOME=C:\TOOLS\apache-tomcat-8.0.30
  
  set CATALINA_BASE=C:\Users\ULUser\tomcat\myapp
  set MY_VAR=%CATALINA_BASE%
  set PATH=%CATALINA_ROOT%\bin;%PATH%
  set CATALINA_OPTS="-Djava.security.properties=%CATALINA_BASE%\conf\java.security.properties"
  
  %CATALINA_HOME%\bin\catalina.bat run
  </code></pre>
  
  <p>But when I run as a windows service I cannot retrieve the MY_VAR variable using <code>System.getenv("MY_VAR");</code></p>
  
  <p>Googling has not helped because all the pages show how to set the Java and Tomcat variables, but not how to set user variables.</p>
  
  <p>I cannot set it for the entire machine because I have several instances of tomcat and each one needs a different environment variable.</p>
  
  <p>I would prefer a method that involves setting the variable in the tomcat configuration rather that with windows configuration.</p>
  
  <p>My alternative is to use context.xml file in tomcat and retrieve the value with logic that retrieves context settings.</p>
  
  <p>I originally posted this on superuser, but it was suggested that stackoverflow is a better place for this question.  If I get good answers here I will delete it off of superuser.</p>