2

I am wondering if there is a way, from java, to find the location of the java.exe running the program?

My use case is that I am trying to have Java execute a batch script that needs JAVA_HOME to be set in the local environment. The environment that I am executing this on may not have JAVA_HOME set or even the java executable on the path, but I would assume that the JVM knows where its executable is located.

2 Answers2

5
 System.getProperty("java.home");

is one option. Which shows the following directory in my machine:

C:\Program Files\Java\jdk1.7.0_02\jre

Most important system properties are listed here.

jaydez
  • 153
  • 8
Sage
  • 15,290
  • 3
  • 33
  • 38
  • 3
    Nice edit completely changing your previous answer. Don't get me wrong, I like right answers, but when it's already been posted elsewhere... – T.J. Crowder Oct 14 '13 at 21:49
  • yeah. so true, i usually do this wrong, rechecked the linked page and then fixed it. ;) Are you the one who down voted me? :) – Sage Oct 14 '13 at 21:50
  • 1
    @ Sage: No. By the time I got here, the incorrect answer had already been downvoted and you'd deleted it. I don't downvote correct answers, even when they really should be deleted because they duplicate other correct answers after the fact. – T.J. Crowder Oct 14 '13 at 21:54
  • After the downvote i rechecked page i have linked and fixed it. But if you think that i picked it from you, i can remove it smilingly if you want. +1 for you. ;) – Sage Oct 14 '13 at 21:55
  • 1
    @ Sage: No worries, the important thing is the OP gets a resolution. Turns out this is a duplicate. – T.J. Crowder Oct 14 '13 at 21:56
  • @T.J.Crowder, I haven't yet used to this site. Though my account was created 1 year before, i am regular here for last 17 days. I am not talking about this one but my answer collided with other question's answer in 1-2 minutes elapse, happened to me several time. I even continue to edit my answer to get things right with details for 5 - 10 times. – Sage Oct 14 '13 at 22:01
  • 1
    @ Sage: Yeah, happens all the time. Usually if I've posted an incorrect answer and later fixed it, but then notice that in the meantime, someone (hexacyanide, in this case) posted a correct one, I'll just delete mine unless it offers more information. – T.J. Crowder Oct 14 '13 at 22:03
  • uhh, can't delete the accepted answer :( why is that ? sorry i have just tried to remove it but it didn't work – Sage Oct 14 '13 at 22:17
4

The java.home property will supply the path to the currently running JRE, regardless of what is installed, or what JAVA_HOME is. See the Oracle documentation here.

System.getProperty("java.home");
hexacyanide
  • 88,222
  • 31
  • 159
  • 162