Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using environment variables in "VM options" and "Program arguments"

Tags:

In my project configuration in IDEA, I have the following text fields:

Project configuration

I would like to define some environment variables, and refer to them in the fields "VM options" and "Program arguments".

I tried with the following definitions for environment variables:

MY_FOLDER=/some/random/path MY_ARGUMENT=2 

and then in "VM options" I entered:

-Dfoo=$MY_FOLDER

and in program arguments I entered

$MY_ARGUMENT foo bar 

However, the environment variables do not seem to be resolved prior to calling my class, i.e. if I inspect args[0] in Java, it holds the string value $MY_ARGUMENT, not 2.

Why? and how can I fix this?

like image 428
Amelio Vazquez-Reina Avatar asked Feb 04 '14 17:02

Amelio Vazquez-Reina


People also ask

What is the difference between program arguments and VM arguments?

Program Argument: Program arguments are arguments that are passed to your application, which are accessible via the "args" String array parameter of your main method. VM Argument: VM arguments are environment or system argument that needed by JVM to execute the program.

What is the use of VM arguments?

VM arguments are typically values that change the behaviour of the Java Virtual Machine (JVM). For example, the -Xmx256M argument allows the Java heap to grow to 256MB. The Eclipse runtime is also configurable via many system properties which can be passed as VM arguments in the form: -DpropertyName=propertyValue.

Where should I set VM arguments in IntelliJ?

From the main menu, select Help | Edit Custom VM Options. If you do not have any project open, on the Welcome screen, click Configure and then Edit Custom VM Options. If you cannot start IntelliJ IDEA, manually copy the default file with JVM options to the IntelliJ IDEA configuration directory.


1 Answers

You can access environment variables using the brace-enclosed environment variable syntax. Example:

VM options: -Dfoo=${MY_ENV_VAR}

VM options

MY_ENV_VAR environment variable will be expanded properly.

Update: tested in IntelliJ IDEA 2017.1.2 and still working.

like image 95
David Miguel Avatar answered Sep 28 '22 11:09

David Miguel