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?

4
  • 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. Commented Jan 17, 2015 at 17:50
  • I am just doing right click then Run Test. Like typical manual JUnit test. Commented Jan 17, 2015 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. Commented Jan 17, 2015 at 17:57
  • 4
    Check out: How do you input commandline argument in IntelliJ IDEA?, this is where you need to put -Dsdk.root=something Commented Jan 17, 2015 at 18:06

1 Answer 1

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=....

Sign up to request clarification or add additional context in comments.

2 Comments

Why not System.getProperty ?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.