Setting up and using environment variables in IntelliJ Idea - 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?

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:

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.

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

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$

Related

How to update JAVA_HOME for the entire system, not just my terminal?

When I swap my JAVA_HOME to point to another env and close terminal and open it back up, I can echo it and it shows the right path.
However some other applications (name IntelliJ IDEA) will hold the first JAVA_HOME until I log my user out. Simply quitting IntelliJ IDEA like terminal doesn't work.
Why is this? I'm curious where does someone like IntelliJ actually get my JAVA_HOME in case I have one set in my bash_profile and my config_fish for instance. How does intellij (and other programs) know where to look?
Not Sure about IntelliJ but in eclipse, you can set your environment in Window > preference > java > Installed JRE and add your path their
Okay, so there are such things as system env variables but in this case intellij wasn't pulling these from my system. Intellij instead knows how to look for your bash_profile among other configs (also zsh config and fish config)
See the source here: https://github.com/JetBrains/intellij-community/blob/master/plugins/terminal/resources/jediterm-bash.in

SBT can't read environment variables in IntelliJ

I defined a new environment variable in ~/.zshrc like that: export JVM_XMX=-Xmx2048M. I can verify that it was set correctly running export command and finding it in the list.
Now I want to use it in SBT. I've tried these two approaches:
sys.env("JVM_XMX")
sys.env.get("JVM_XMX")
But the value couldn't be found or the Option is None. Errors that I see are:
NoSuchElementException: key not found: JVM_XMX
NoSuchElementException: None.get
What I also tried was to add the variable into SBT in IntelliJ Settings. I went to Build, Execution, Deployment -> Build Tools -> sbt and set VM parameters to -DJVM_XMX=-Xmx2048M. It didn't help.
Anyone knows how to setup SBT to work with IntelliJ correctly?
Versions used:
sbt 1.2.8
IntelliJ IDEA 2019.2.1
As a workaround I was able to use system properties (scala.sys.SystemProperties). This works because this is the way how to find values added into SBT in IntelliJ Settings.
Code example from build.sbt:
sys.props.get("JVM_XMX")
UPDATE:
I was finally able to figure out what was the real problem. My .bashrc file was incorrectly set up (I had the variables only in .zshrc). After adding environment variables into correct rc file, the problem was fixed.
If you'd like this property to be part of your project, and not only in your solution, you can add a file names ".sbtopts" at the root of your repository, next to the build.sbt file.
In this file you can configure the JVM options.
For instance you can add there:
-J-Xmx2048M
I couldn't find the sbt documentation supporting my suggestion, but it works for me :)
I was able to work around this issue by going to Preferences -> Build, Execution, Deployment -> Build Tools -> sbt, then enabling sbt shell for builds and project reload.

IntelliJ 14 missing HOME env. variable

Whenever I start IntelliJ I see a notification balloon pointing out that I have an undeclared HOME variable. I can remove it but the same thing happens when I open IntelliJ again. I would like to fix it but I have no idea which HOME variable it is referring to.
The project is a Grails 2.4 project with a JDK7 on Windows 7.
Any suggestions?
Have you tried to set this enviroment variable in your windows system settings?
I think the name of this variable must be M2_HOME and must point to the path of your maven installation.
start -> right click on computer -> extended system settings -> enviroment
See also: http://maven.apache.org/download.cgi under section "Installation Instructions" and "Windows".

How to set environment vars in IntelliJ for Gradle tasks

the easiest way to pass spring profiles to gradle bootRun is (for me) by environment variable (e.g. SPRING_PROFILES_ACTIVE), when run on commandline.
Unlike the Application configurations, the config for gradle tasks does not provide an input for environment variables. And as VM options don't get picked up either as it seems, I can not run those tasks from the IDE.
I am aware, that I could start IntelliJ with the envvar set, but this seems rather cumbersome.
So what I need is the IntelliJ pendant for SPRING_PROFILES_ACTIVE=dev,testdb gradle bootRun, unless there is a good reason, they have left this out.
System is linux, intellij 14. The project in question is using springboot and I want to move over from running main in IntelliJ to running with springloaded+bootRun and separate compileGroovy calls as IntelliJ is not "understanding" the gradle file completely and therefor hides errors.
Make the System.properties available in the bootRun (or other) tasks.
bootRun.systemProperties = System.properties
This way we can set in IntelliJ VM Options like -Dspring.profiles.active=dev.
Here is my solution for setting up Spring environment variables / settings with Gradle / IntelliJ
Firstly, define a basic properties file, and then one based on your environment, such as:
#Configuration
#PropertySources(value = {#PropertySource("classpath:default.properties"),#PropertySource("classpath:${env}.properties")})
Int the above example, pay careful attention to the #PropertySource("classpath:${env}.properties"). This is an environment variable being pulled through.
Next, add a VM argument to IntelliJ (via the Gradle Tasks Run Configurations) - or as an argument via the gradle command line.
Lastly, copy the properties across in the gradle task as #cfrick mentioned and #mdjnewman have correctly shown:
tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
bootRun.systemProperties = System.properties
}
I've had success adding the following to my build.gradle file:
tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
systemProperty('spring.profiles.active', 'local')
}
This allows gradlew bootRun to be run from IntelliJ without requiring any changes to the IntelliJ Run/Debug Configurations (and also from the command line without having to manually specify a profile).

How to set environmental variables in IntelliJ IDEA?

How (where?) can I set Environment Variables using IntelliJ idea?
For instance, I am looking forward to set the $APP_HOME (my tests rely on).
Where can this be done please?
Most run configurations have an option to set environment variables:
The build settings window has a configuration tab with an option to change the environment variables.
http://www.jetbrains.com/idea/webhelp/run-debug-configuration-application.html
Otherwise you can use system system calls or the java language itself to change them during runtime.
How do I set environment variables from Java?