Checking/getting JAVA_HOME Variable from Java

16,909

Solution 1

Since both PATH and JAVA_HOME are environment variables, you should be able to read both of their values in a similar way:

String javaHome = System.getenv("JAVA_HOME");
String path = System.getenv("PATH");

Solution 2

Try

String javaHome = System.getProperty("java.home");

Solution 3

Use System.getenv() to read the value.

 System.getenv("JAVA_HOME");

Solution 4

You have to use System.getenv("JAVA_HOME");

Share:
16,909
antonio
Author by

antonio

I use Emacs and R for financial research Consider to post or follow the virtualization proposal.

Updated on June 21, 2022

Comments

  • antonio
    antonio almost 2 years

    Inside a Java program, how can I read the JAVA_HOME variable (to be sure it is set the correct way)? Similarly, how can I get the path of the bin folder? That is, the path usually set in Windows via:

    path %path%;%JAVA_HOME%\bin

    Note: I am using the OpenJDK build by Alexkasko.

  • antonio
    antonio almost 11 years
    I get: String javaHome = System.getProperties("java.home"); ^ required: no arguments found: String reason: actual and formal argument lists differ in length 1 error
  • hansvb
    hansvb almost 11 years
    Sorry, it's getProperty, not getProperties.
  • golimar
    golimar over 8 years
    Thanks, System.getenv("JAVA_HOME") didn't work for me as the variable was not exported (non-environment variable)
  • David
    David almost 8 years
    Java properties and environment variables are two different things, and from the question it looks like he specifically asked for the latter.