Reputation: 4279
I need to use an environment 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.
Upvotes: 54
Views: 98633
Reputation: 328
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.
Upvotes: 23
Reputation: 3019
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.
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!
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.
Upvotes: 18
Reputation: 281
Where to create environment variable
IntelliJ IDE > Edit Configuration > Environment Variables (Right button) > enter "key" and "value" (Creates configuration)
How to use in JAVA
System.getenv("key")
to retrieve env value
How to create multiple environment
create multiple configurations :)
Upvotes: -1
Reputation: 48
@Dávid Leblay's answer does the trick. Just expanding on it here as I do not have enough rep to comment...
I'm on Linux Mint, and finding the location to my IntelliJ .desktop entry was tricky. Want to share the steps in case anyone else comes across this:
Use this command to properly identify the filename of your IntelliJ desktop entry:
sed -n '/pinned-apps.*{/,/}/p' ~/.cinnamon/configs/[email protected]/*.json
Mine ended up being "jetbrains-idea.desktop"
With the file name, you need the full path so that you can edit the entry. Find the path via this command: find / -name '*.desktop'
-- (Including this because I went to .local/share/applications/
as @sherminator35 noted for Ubuntu and my entry was not there)
Replace the * with your filename, e.g. find / -name 'jetbrains-idea.desktop'
With the path, edit the entry as @Dávid Leblay noted with vi or vim: vim /path/to/entry/jetbrains-idea.desktop
With that, IntelliJ should now properly pick up your env variables without having to edit the run config.
Resources that helped me:
Upvotes: 0
Reputation: 377
Open 'Edit Run/Debug configurations' dialogs
Modify options
Environment variables
Environment variables
below Active profiles
Upvotes: 2
Reputation: 211
I found another tricky solution :)
At least for Linux users...
Just create some shell script like idea.sh in any suitable location with this content.
#!/bin/bash export YOUR_ENV_VARIABLE=some value cd ~/path_to_your_idea_folder/bin bash ./idea.sh
Make this script executable and run it.
This script will always run your IDE with predefined env variables.
Upvotes: 1
Reputation: 93
As no other answer mentioned it here,
Add your environment variable to /etc/environment , then log out and log in again. IntelliJ will definitely pick it up.
Upvotes: 2
Reputation: 2716
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
Upvotes: 2
Reputation: 1303
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: Run → Edit 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.
Upvotes: 11
Reputation: 6288
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.
Upvotes: 11
Reputation: 79
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.
Upvotes: 7