28

I need to use an envirnoment variable in all of my idea run configurations. I currently use run->edit configurations->and then enter the env variables in selected configuration. However that's very tedious when I need to run isolated test scenarios because each one creates a new run configuration and I need to enter the variables all over again.

I tried to set the env variables in my linux system using export SOME_VAR="some value" in various session profile files: /etc/profile,/etc/bash.bashrc,~/.bashrc,~/.profile but intellij seems to ignore those vars during run, even though when I launch echo ${SOME_VAR} from intellij built-in terminal it displays the correct output.

I also tried using intellij .env file plugin and then set SOME_VAR=some value in .env file in project root. Didn't work either.

CrazyCoder
  • 350,772
  • 137
  • 894
  • 800
Ben
  • 3,051
  • 5
  • 38
  • 70
  • Did you try editing the configuration under the `Default` node so that this setting automatically applies to all the new Run/Debug configurations of this type you create later? – CrazyCoder Aug 15 '17 at 22:26
  • I've noticed that IntelliJ honors the `/etc/environment` configuration file... – Makoto Aug 15 '17 at 22:27
  • See https://youtrack.jetbrains.com/issue/IDEABKL-7589 regarding what environment IntelliJ IDEA can load on Linux. – CrazyCoder Aug 15 '17 at 22:30

6 Answers6

14

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:

enter image description here

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":

enter image description here

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

enter image description here

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

enter image description here

That's it!!!

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

enter image description here

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:

enter image description here

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):

enter image description here

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.

Enjoy!

...

sflr
  • 2,177
  • 1
  • 25
  • 22
  • 1
    The Run Configuration you show here is specific to the application main class, but the OP said he is not talking about a main class but about a large number of unit test methods. Each of these will create a new, separate run configuration. – Klitos Kyriacou Sep 25 '19 at 10:18
9

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
Dávid Leblay
  • 91
  • 1
  • 2
  • This was the easiest solution that worked for me. In my case it was for Goland: Exec=bash -i -c "/home/flapjack/.local/share/JetBrains/Toolbox/apps/Goland/ch-0/202.6948.9/bin/goland.sh" %f – flapjack Sep 01 '20 at 20:20
  • That should be the most voted answer... It's easy and works – Júlio Moura Oct 19 '20 at 15:54
  • Yes, this answer should be most voted. For Ubuntu 18.04 and IntelliJ 2020.3, the .desktop file is at `.local/share/applications/jetbrains-idea-ce.desktop` – sherminator35 Feb 05 '21 at 08:48
7

If Maven is used project specific environment variables can be configured under File->Settings->Build, Execution, Deployment->Build Tools->Maven->Runner These are reused then in any Maven configuration.

The same mechanism to set the environment variables might also work with different runners.

k_o_
  • 2,597
  • 19
  • 26
  • this worked for me because I didn't want to pollute my system environment with variables that I'll only be using for one project – Subaru Tashiro Apr 26 '19 at 04:31
  • This will only work for maven executions (via IDE) - if you run code directly via IDE, you need something else. – Tuukka Mustonen Sep 04 '19 at 09:29
  • This didn't work for me, until I put it in the Spring Boot Run/Debug configuration. – Elbbard Jun 16 '20 at 17:57
  • @Servietsky: Has the Spring Run/Debug configuration any relationship to Maven? As the answer is referring to Maven I'm wondering what is the expectation when a different runner is used. If the IntelliJ runner has a similar UI for different runners what works for the Maven set-up might also be beneficial for other runners. If this is the case I would add this note. – k_o_ Jun 16 '20 at 20:15
  • My project uses Maven and, as far as I know, the command `mvn spring-boot:run` is used to start a Maven based spring project. I wanted to mention that using the global (Runner) Maven environment variables have not been working for me on Spring. But maybe the `Spring Boot` configuration template uses other mechanisms I don't know of and it would probably work using Maven `spring-boot:run` directly. – Elbbard Jun 17 '20 at 07:29
5

The problem is, that IntelliJ does not "see" the environment variables that are set in .bashrc (Also to be found in CrazyCoders answer). The easiest way to enable IntelliJ to import those variables is to start it from bash e.g. by typing intellij-idea-community.

Mat
  • 59
  • 1
  • 2
  • 1
    Running on RHEL 7.x and this was the issue. Gnome doesn't run the applications from within a bash shell, so starting the IDE from the menu caused this issue. – Vaiden Jan 08 '20 at 06:58
1

I tried various things listed above, and adding the environment variables to the terminal configuration and the Maven build tools worked in some contexts but not others. Then I finally found the place in IntelliJ that actually works for runtime processes. Because why just have one environment variable configuration screen when you can have several and make all but one of them wrong? ^_^

If you edit the template from which your run configurations are created, and add the environment variables to the template, then they should be included in every subsequent run configuration that started with that template.

This is especially useful for the JUnit template, since it will mean that all your custom environment variables will be loaded for unit tests, regardless of the scope from which they're executed (single method, whole test class, whole module). But in general, if you edit the templates first, then any run configuration you create thereafter will inherit your environment variables from the template.

From the top menu: RunEdit Configurations... → expand Templates tree → (choose a template)Environment variables:(enter a semicolon-delimited key-value pair list OR use the input widget)

For the auto-generated JUnit configurations, you should blow away any existing ones, and let IntelliJ recreate new ones as you go; each of these will use the updated JUnit template with your environment variables.

zerobandwidth
  • 1,005
  • 9
  • 17
0

For macOs try adding /Applications/IntelliJ IDEA.app/Contents/bin/idea.properties

...
apple.awt.graphics.UseQuartz=true
apple.awt.fullscreencapturealldisplays=false
idea.jre.check=true
SOME_VAR=some value
hevi
  • 1,842
  • 1
  • 26
  • 41