简体   繁体   中英

Intellij IDEA: run/debug java application with a set of environment variables

My java application uses environment variables ( System.getenv ), and this set of variables is large and defined in a file like:

export VAR1="VAL1"
...
export VAR100500="VAL1500"

when i start the application from command line i use something like this:

source ../.config/.secret-secret && java  -jar build/libs/app-name.jar

and the values are exported right before the launch (actually, the source command may be executed only once per session, since the values are exported)

But when i start the application from IDEA, reasonably those values are not exported to the run environment.

So, the question is: how can i export a set of environment variables to application launch environment in IDEA?

So, what i've tried already:

  • in Run/Debug Configurations -> Before Launch i started a shell script which exports those variables (i use the same script to run the app from command line). Script executed successfully, but looks like the script is executed in different environment from the application itself, so values are exported (and printed to IDEA run console), but the application still can't read them from System.getenv

  • i can't easily use Run/Debug Configurations -> Environment Variables , because there are a lot of variables to set up manually, also they are changed from time to time.

  • It's not enough to run entire IDEA with those variables, because, as i mentioned above, the values are changed often and should be re-read almost every launch.

I can tell you that editing the workspace.xml file (which is where the environment variables entered in the run configuration are stored) directly does not work, so don't bother trying that.

I think you are going to have to write a plugin that parses your script and updates the run configuration. I haven't written a plugin to do this myself so can't give you a fuller answer. You can see the relevant plugin in documentation here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM