James Leonard
James Leonard

Reputation: 3753

Setting up and using environment variables in IntelliJ Idea

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?

Upvotes: 74

Views: 235645

Answers (6)

CrazyCoder
CrazyCoder

Reputation: 401915

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

If you want to specify default environment variables for every new unit test configuration (as per this answer):

Run > Edit Configurations > Edit Configuration Templates > TestNG/JUnit > Environment Variables

Upvotes: 110

treef
treef

Reputation: 27

This answer extends the answer by CrazyCoder and also the shows the latest IntelliJ IDEA UI. Click on More Actions -> Edit -> Modify options -> Choose Environment Variables from the drop down.

enter image description here

enter image description here

enter image description here

Upvotes: 1

Özgün
Özgün

Reputation: 432

This is not a direct answer to the question, but more an answer to the questions in the comments: "how to reload the env variables when they change ?".

If your env variables are comming from the OS, you unfortunately don't have a choice, as already said by @Neil McGuigan.

But if it comes from another key management tool like HashiCorp Vault, there is a maven plugin that allows fetching and injecting them into your JVM.
https://homeofthewizard.github.io/vault-maven-plugin/
So if you run your app as a maven goal on intelliJ, your env variables will be accessible.
Plus, It's config is project wide since it will be in the pom.xml, so no need to specify your env variables for each run/debug on intelliJ.

Upvotes: 0

Freddie
Freddie

Reputation: 1075

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

Upvotes: 12

sam
sam

Reputation: 4397

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

Upvotes: 4

Crump
Crump

Reputation: 31

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$

Upvotes: 3

Related Questions