2

I need that my code can get the environment variable when running test with IntelliJ:

private String sdkDir = System.getProperty("sdk.root");

The problem is that this is always null, I have set both in bashrc and also with bash_profile but not really works with Ubuntu.

bashrc and bash_profile both have this:

export JAVA_HOME=/home/xybrek/zulu1.8.0_25-8.4.0.1-x86lx64
export JAVA_OPTIONS="-Dsdk.root=/home/xybrek/java-sdk-1.9.17"
export PATH=$PATH:/home/xybrek/zulu1.8.0_25-8.4.0.1-x86lx64/bin
export PATH

What could be missing with my configuration?

Update:

I cannot change the Java code: System.getProperty("sdk.root"); as this is coming from a compiled Arquillian container I can't modify.

What I need would be a proper way of having this System.getProperty method to get the value.

Where to put this "sdk.root" for IntelliJ to pick it up? In a properties file or bash?

quarks
  • 33,478
  • 73
  • 290
  • 513
  • What is the exact Java command line you're running? It looks like you need to explicitly include `$JAVA_OPTIONS` in the command line invocation - the Java process won't automatically read that environment variable. – nobody Jan 17 '15 at 17:50
  • I am just doing right click then Run Test. Like typical manual JUnit test. – quarks Jan 17 '15 at 17:52
  • There must be a way of setting the command line options directly in some IntelliJ dialog, for running some specific Java app. Nothing to do with environment variables. – laune Jan 17 '15 at 17:57
  • 4
    Check out: [How do you input commandline argument in IntelliJ IDEA?](http://stackoverflow.com/questions/2066307/how-do-you-input-commandline-argument-in-intellij-idea), this is where you need to put `-Dsdk.root=something` – eis Jan 17 '15 at 18:06

1 Answers1

5

You have to use

String val = System.getenv( "PATH" );

for the value of an environment variable. A property value has nothing to do with the Environment of a process.

String val = System.getProperty( "sdk.root");

is for properties.

There should be a way of setting command line arguments in the dialog for preparing the execution of a Java program in your IDE. This is where you should define -Dsdk.root=....

laune
  • 31,114
  • 3
  • 29
  • 42
  • Why not System.getProperty ? – quarks Jan 17 '15 at 17:46
  • Check your IDE's features. In Eclipse, the run dialog has a tab for defining the environment and another one for command line options where you could put the -Dsdk.root=... literally. The environment variable will only work if some agent expands it in the command line calling java, the JVM. – laune Jan 17 '15 at 17:52