Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking/getting JAVA_HOME Variable from Java

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.

like image 485
antonio Avatar asked Apr 30 '13 10:04

antonio


4 Answers

Try

String javaHome = System.getProperty("java.home");
like image 118
Thilo Avatar answered Sep 30 '22 07:09

Thilo


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");
like image 25
David Avatar answered Sep 30 '22 08:09

David


Use System.getenv() to read the value.

 System.getenv("JAVA_HOME");
like image 38
AllTooSir Avatar answered Sep 30 '22 08:09

AllTooSir


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

like image 43
Andrea Boscolo Avatar answered Sep 30 '22 08:09

Andrea Boscolo