Select Run > Edit Configurations... then on the bottom-left of the dialog click Edit Configuration Templates in the left pane select JUnit then in the right use Environment Variables: to add your environment variables. Click Apply or OK. From now on, those variables will be added to every new JUnit run configuration in your project.

Top Tip If you click the Edit environment variables, you can paste multiple env variables at once like this:

FOO=bar
BAR=foo

Answer from Darek Kay on Stack Overflow
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 205820189-How-to-set-default-environment-variables
How to set "default" environment variables? – IDEs Support (IntelliJ Platform) | JetBrains
June 30, 2015 - Please, go to `Run| Edit Configurations| Defaults| Python tests| Unitests/Nosetests/...` and specify your environment variables in "Environment variables" field, then apply changes.
Discussions

IntelliJ IDEA global environment variable configuration - Stack Overflow
I found a solution to set environment variables on IntelliJ that has been working very well for me, and is incredibly simple. Let me show you. This is the program (you can copy and paste it) we're using to test: More on stackoverflow.com
🌐 stackoverflow.com
Setting up and using environment variables in IntelliJ Idea - Stack Overflow
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 ... Sign up to request clarification or add additional context in comments. ... Make sure you restart IntelliJ ... More on stackoverflow.com
🌐 stackoverflow.com
Add support for setting Environment Variables via Run Configuration
This may be a naive request since I have no knowledge Intellij's Plugin API, but would it be possible to add a field in Bazel run configurations to be able to set ad-hoc environment variables f... More on github.com
🌐 github.com
18
August 14, 2020
jestjs - JetBrains IntelliJ/WebStorm, how to persist environment variables across all tests runners - Stack Overflow
I am using IntelliJ and/or WebStorm. I'm working on an app that have hundreds of tests (Jest). Theses tests need environment variable to run (stored in .env.test file). Every time I start a new test by clicking the little arrow in the file, I have to reconfigure all the environment variables ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Emmanuel Bernard
emmanuelbernard.com › blog › 2012 › 05 › 09 › setting-global-variables-intellij
Setting global environment variables in IntelliJ IDEA and other test config goodies - Emmanuel Bernard
May 8, 2012 - Alternatively, you can set an environment variable globally in IntelliJ - what they call parent environment variables. Go to Preferences and search for Path Variables. Set your global environment variables here and you are good to go.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 206157969-How-to-set-environment-variables-for-test-config
How to set environment variables for test config? – IDEs Support (IntelliJ Platform) | JetBrains
package demo import spock.lang.Specification class ReadEnvVarSpec extends Specification { void "when I read environment variables those set in IDEA run configuration should be available"() { given: // test run configuration in IDEA has env var 'FOO' set to 'BAR' (without quotes) // Dump them all out for manual verification of issue System.getenv().sort().each {k,v -> println "$k = $v"} when: String result = System.getenv('FOO') then: result == 'BAR' } }
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360000195304-How-to-set-environment-variables-generated-external-tool-before-running-test-runner
How to set environment variables generated external tool before running test runner – IDEs Support (IntelliJ Platform) | JetBrains
Since I couldn't use a script that is launched before my run/debug in the same terminal so I can reuse the env variables, and that the envfile plugin cannot use a script that generates the file, I ended up using a maven plugin to generate the env variables, and run my app as a maven goal on intellij. Here is the doc: https://homeofthewizard.github.io/vault-maven-plugin/ It is basically creating env variables or system properties in the JVM of maven, and if you execute your app in the same JVM, your app will be able to access them. It is IDE agnostic, pure java solution that works on every environment (docker, local windows/linux/mac).
🌐
LaunchCode
education.launchcode.org › gis-devops › configurations › 02-environment-variables-intellij › index.html
Configuration: Environment Variables - IntelliJ — LaunchCode: GIS DevOps documentation
In this configuration we will learn how to create new environment variables in IntelliJ, and we will learn the syntax for calling those variables in our code.
Top answer
1 of 11
26

In my opinion the real issue is what Mat said. If you want to launch IntelliJ from a shortcut, then you have to edit it a little bit: Open the .desktop file, and add /bin/bash -c -i to the beginning of the launch command. The file should look something like this:

[Desktop Entry]
Exec=/bin/bash -i -c "/path/to/idea/bin/idea.sh" %f
Name=IntelliJ IDEA Ultimate
Type=Application
Version=1.0

The desktop file in ubuntu can be located at:

  • .local/share/applications/jetbrains-idea-ce.desktop

If the IDEA is installed via snap, then copying the desktop file is needed:

cp /var/lib/snapd/desktop/applications/intellij-idea-ultimate_intellij-idea-ultimate.desktop ~/.local/share/applications/

After editing the desktop file in ~/.local/share/applications/ the desktop menu should refresh itself in a few seconds.

2 of 11
19

I found a solution to set environment variables on IntelliJ that has been working very well for me, and is incredibly simple. Let me show you.

This is the program (you can copy and paste it) we're using to test:

package com.javasd.intelijenv;

import java.util.Map;

public class Main {
    public static void main(String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n", envName, env.get(envName));
        }

        System.out.println("My home directory: " + System.getenv("MY_VAR"));
    }
}

This program basically loads all environment variables, show them on the console, and try to show an env variable. Also, it assumes that you had created the MY_VAR env variable before calling IntelliJ IDEA, by doing something like:

$ export MY_VAR="This is my adorable var :)"
$ idea

Please, notice that we're calling IntelliJ IDEA in the same terminal (or session, or window) where we created the environment variable. If you create the variable and call the IDEA from the icon, the solution won't work because the IDEA will create its own session.

So, if run it without the correct configuration you will get something line this in your console:

Please, notice that you have just a few variables, and that MY_VAR is null.

Here's configuration I use to load the environment variables:

  1. Click on the "Select Run/Debug Configurations" in your project and select "Edit Configurations":

  1. Then, click on the the button with "..." on the right side of the "Environment Variables" section:

  1. You'll see a pop-up. Check the checkbox on the bottom-left which has the label "Include parent environment variables":

That's it!!!

If you run your program now you will see something like this on your console:

You can see all the environment variables and, of course, your "MY_VAR" variable, with the right value!

Beyond the Basics

Usually, for security reasons, we don't want to keep all the environment variables visible. What we want to do is to make those variables visible only while the IntelliJ (or our program) is running.

So, no sensitive variables should be visible on the environment neither before you call Intellij nor after you close it.

Also, you want to keep those variables in a file (typically with a .env extension) to make it easy to manipulate and for security reasons.

To achieve this, there are some useful programs (you can Google them), but my favorite one is the env-cmd.

Let's say you have a test.env file with the following content:

MY_TEST_VAR=I live in the test.env file.

If you call IntelliJ by doing this:

$ env-cmd test.env idea

And edit your program to show "MY_TEST_VAR", and run it, you will see this on the IntelliJ's console:

But if you quit the IntelliJ, and look for your variable, you will see that the var doesn't exist (you can use env to confirm):

At this point, I hope you're able to play with your own solutions: create shell scripts with variables set inside, test other programs (direnv, autoenv, etc.), and so on.

Find elsewhere
🌐
Baeldung
baeldung.com › home › ide › how to set up environment variables in intellij idea
How to Set Up Environment Variables in IntelliJ IDEA | Baeldung
May 11, 2024 - In the above process, we added the environment variable testUser baeldung into a project in IntelliJ by making changes in the run configuration. We can also add environment variables to a configuration file and link it to a project by editing the run configuration of an application. To demonstrate, let’s look at the process of editing the configuration file for ...
🌐
JetBrains
jetbrains.com › help › idea › run-debug-configuration-attests.html
Run/Debug Configuration: attests - IntelliJ IDEA
September 1, 2023 - You will be redirected shortly · Redirecting… · Click here if you are not redirected
🌐
Diffblue
docs.diffblue.com › features › cover-plugin › writing-tests › run-configurations
Run configurations | Diffblue Documentation
Test creation target - Select All in package, Class, Method, or Prefixes from the drop down to set the scope of items you're writing tests for. Enter the target details as appropriate. Write skeleton tests - If selected, Cover Plugin will write skeleton tests instead of creating complete tests. See Creating skeleton tests for more information. Environment Variables - Provide a list of key/value pairs to set environment variables.
🌐
GitHub
github.com › bazelbuild › intellij › issues › 2042
Add support for setting Environment Variables via Run Configuration · Issue #2042 · bazelbuild/intellij
August 14, 2020 - This may be a naive request since I have no knowledge Intellij's Plugin API, but would it be possible to add a field in Bazel run configurations to be able to set ad-hoc environment variables for a given executable? For example, the Java run configurations have this field available allowing for environment variables to be set per run configuration:
Author   bazelbuild
🌐
JetBrains
jetbrains.com › help › idea › program-arguments-and-environment-variables.html
Program arguments, VM options, environment variables | IntelliJ IDEA Documentation
May 27, 2025 - In the Run/Debug Configurations dialog, open the run configuration, for which you want to modify environment variables. In the run configuration settings, use the Environment variables field to set the environment for your application:
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360000497650-Cant-set-default-environment-variables-in-Goland-Run-Debug-configs
Cant set default environment variables in Goland Run/Debug configs – IDEs Support (IntelliJ Platform) | JetBrains
May 9, 2018 - SONOBUOY_CLI=/Users/jschnake/go/src/github.com/vmware-tanzu/sonobuoy/sonobuoy GOTARGET=github.com/vmware-tanzu/sonobuoy KUBECONFIG=/Users/jschnake/.kube/config a=b; go test $GOTARGET/test/integration -update -v -tags integration -run 'TestPluginsPersistThroughLogQuery' ... ... ENVIRON IS... [PATH=/usr/local/go/bin:/Users/jschnake/go/bin:/Users/jschnake/bin:/usr/local/bin:/Users/jschnake/go/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin TERM=xterm-256color COMMAND_MODE=unix2003 __INTELLIJ_COMMAND_HISTFILE__=/Users/jschnake/Library/Caches/JetBrains/GoLand2021.2/terminal/histo
🌐
JetBrains
youtrack.jetbrains.com › issue › IDEA-144859 › Environment-variables-not-being-set-in-run-config
Environment variables not being set in run config
{{ (>_<) }} This version of your browser is not supported. Try upgrading to the latest stable version. Something went seriously wrong
🌐
Stack Overflow
stackoverflow.com › questions › 38527766 › environment-variables-not-set-when-running-java-integration-tests
intellij idea - Environment variables not set when running Java integration tests - Stack Overflow
I would report this to intellij as a bug! 2016-07-26T13:28:21.96Z+00:00 ... Our current working solution to create environment variables for our junit tests is to run the command "launchctl setenv USERNAME mrSmith".
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 9865478654866-How-to-ACTUALLY-set-environment-variables-in-PyCharm
How to ACTUALLY set environment variables in PyCharm? – IDEs Support (IntelliJ Platform) | JetBrains
January 31, 2023 - The current workaround is to add an EnvFile config for every individual test! It seems like Run>Edit Configurations does absolutely nothing unless I specify the exact file_name/python_function.