System.getProperty driving me nuts
Is it just me or is there a problem with IDEA reading properties during run/debug? I swear I've done this 100 times and while it works fine from the command line it doesn't appear to work in the IDE. I've tried my 10 CE and my new Ultimate 13 installs with the same results.
public class PropertyTester {
public static void main(String[] args) {
String property = System.getProperty("property1");
System.out.println("property = " + property);
String property2 = System.getProperty("property2");
System.out.println("property2 = " + property2);
String property3 = System.getProperty("property3");
System.out.println("property3 = " + property3);
}
}
Program parameters: -Dmy-property1=abc -Dmy-property2= -Dmy-property3=ghi
What IDEA prints:
property1 = null
property2 = null
property3 = null
What command line prints when executing the jar:
property1 = abc
property2 =
property3 = ghi
What in the world am I missing? Maybe I've been staring at it too long.
Please sign in to leave a comment.
1. there's property1 in code and my-property1 in parameters, the same for all props
2. you need to pass "-Dmy-property1=abc -Dmy-property2= -Dmy-property3=ghi" not as program arguments, but as VM options. See Run | Edit Configurations...
Put it into "VM options", because these are not program arguments, but VM arguments. Also property names are wrong, try these:
-Dproperty1=abc -Dproperty2=foo -Dproperty3=ghi
...too late. :8}
Good grief. The property name was an oversight but where I was putting them was just plain silly. I knew I was staring at it too long. I didn't even realize it and I was so focused I missed the forest for the trees.
Thanks guys. :)
Hello from the future. It's 2021 and I just spent an embarrassing amount of time on the same problem.
@...
The initial problem has been answered and the user already confirmed that putting properties into VM options solved this.
So, what is the problem you faced? May you describe it from the beginning?
Hi Konstantin. The problem I had was the same as OP, I confused "VM Options" with "Program Arguments" in the run configuration editor. I'm not sure if I have any suggestions. I did notice that "VM Options" weren't available to be edited until they're added, if they were there from the start it may have been less confusing.