69

I set up an environment variable (Under IDE Settings -> Path Variables)

 APP_HOME = /path/to/app_home  

One of my tests is failing however with

 System.out.println("APP HOME: " + APP_HOME); 

With

 APP HOME: null/ 

It does not look like that env variable is being read. What am i missing?

isapir
  • 21,295
  • 13
  • 115
  • 116
James Leonard
  • 3,593
  • 5
  • 27
  • 32

4 Answers4

109

Path Variables dialog has nothing to do with the environment variables.

Environment variables can be specified in your OS or customized in the Run configuration:

env

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 27
    Make sure you restart IntelliJ after changing a system environment variable. It'd be nice to have a "reload" feature! – Neil McGuigan Oct 15 '13 at 20:44
  • This field appears to be disabled when the what appears to be newer "Use project settings" option is checked. Is there a way to specify the environment variables in the "project settings"? – jpierson Jul 28 '17 at 15:21
  • @jpierson I don't have such option, please provide a screenshot or more details. – CrazyCoder Jul 28 '17 at 15:23
  • Will these environment variables available to Gradle at build time? – jpierson Jul 28 '17 at 15:24
  • No, the setting is only for this specific run configuration. – CrazyCoder Jul 28 '17 at 15:25
  • @jpierson for Maven, when project setting option is enabled, run configuration will use the settings that are specified [here](http://i.imgur.com/y8pZx3k.png). But it's offtopic since the original question was about application configuration, not Maven. If you have a different question, submit a new one. – CrazyCoder Jul 28 '17 at 15:31
  • 27
    It's tedious and error-prone setting up several env vars in each run config. There needs to be a way to set them for the entire project. – Brent Faust Nov 04 '17 at 16:12
  • is it possible to put here `/etc/environment`? – valijon Dec 19 '18 at 13:54
  • image is broken – Lei Yang Jun 12 '19 at 02:41
  • 1
    @LeiYang it's not, your ISP is probably blocking the image host used by SO. – CrazyCoder Jun 12 '19 at 02:43
  • But how to specify an environment variable for Tomcat server, say CATALINA_HOME? It config fields lack the field for environment variables. Eclipse does have the field. https://stackoverflow.com/questions/26350970/tomcat-log-is-not-created-when-the-application-and-server-are-started-through-ec – WebComer Aug 08 '19 at 15:35
  • @WebComer you can [pass environment variables](https://i.imgur.com/M5uo9SM.png) for Tomcat in IntelliJ IDEA. – CrazyCoder Aug 09 '19 at 07:33
  • @Sixro please share the screenshot like the one in the answer to show the problem. – CrazyCoder Jun 09 '21 at 17:27
7

I could not get environment variables to work when IntelliJ Build and run property was using Gradle. I am not sure what the root cause is, but switching to IntelliJ IDEA solved the problem. Go to Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle. Then change Build and run using: to IntelliJ IDEA.

IntelliJ Run Preferences

Freddie
  • 908
  • 1
  • 12
  • 24
4

If the above answer + restarting the IDE didn't do, try restarting "Jetbrains Toolbox" if you use it, this did it for me

sam
  • 4,357
  • 5
  • 29
  • 34
3

It is possible to reference an intellij 'Path Variable' in an intellij 'Run Configuration'.

In 'Path Variables' create a variable for example ANALYTICS_VERSION.

In a 'Run Configuration' under 'Environment Variables' add for example the following:

ANALYTICS_LOAD_LOCATION=$MAVEN_REPOSITORY$\com\my\company\analytics\$ANALYTICS_VERSION$\bin

To answer the original question you would need to add an APP_HOME environment variable to your run configuration which references the path variable:

APP_HOME=$APP_HOME$
Crump
  • 31
  • 3
  • 3
    Ignore the above. When you restart intellij it will substitute the path variable in your run configs with a hard coded value. Thanks intellij. – Crump Dec 17 '19 at 11:17