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");
Author by
antonio
I use Emacs and R for financial research Consider to post or follow the virtualization proposal.
Updated on June 21, 2022Comments
-
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 thebin
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 almost 11 yearsI get:
String javaHome = System.getProperties("java.home");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
1 error
-
hansvb almost 11 yearsSorry, it's getProperty, not getProperties.
-
golimar over 8 yearsThanks, System.getenv("JAVA_HOME") didn't work for me as the variable was not exported (non-environment variable)
-
David almost 8 yearsJava properties and environment variables are two different things, and from the question it looks like he specifically asked for the latter.