'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



Solution 1:[1]

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

Solution 2:[2]

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 sbt shell use for settings

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tomer Shetah
Solution 2 Snowbuilder