Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find absolute java.exe path programmatically from java code

Tags:

If I have a java jar or class file which is launched by the user (assuming java path is set in environment variables), so how can i from within the code, figure out absolute path of java.exe/javaw.exe from which this file is being launched.

Like on ubuntu we can run: % which java and it shows the path.

However on windows, if i check System.getenv() it may happen that there are multiple path's found e.g for old or new version. If through cmd line, I run java -version it does not show the path.

Can you tell me either through pure java or command line on windows how is it possible to find out the location of javaw.exe?

like image 438
Johnydep Avatar asked Jan 25 '12 16:01

Johnydep


People also ask

How do I find my Java executable?

You need to find the Java executable file (java.exe file) on your computer hard drive. It is often located in the "Program Files\Java" or "Program Files (x86)\Java" folder, within a possible subfolder below the Java folder. Once you find the file, select it and click OK.

What is the path to Java EXE?

The java.exe Executables Two copies of the java.exe executable are installed. One copy is in the bin directory of the Java RE. The second copy is placed in either C:\windows\system or C:\winnt\system32 , depending on the system.

How do I find Java location?

Open File Explorer (Win⊞ + R, type explorer, hit Enter), and browse for your Java installation folder. It will likely be installed in either Program Files\Java or Program Files (x86)\Java on your OS drive. Once you find your Java folder, Open to it.

How do I get real path?

The realpath() function returns the absolute pathname. This function removes all symbolic links (like '/./', '/../' and extra '/') and returns the absolute pathname.


1 Answers

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

Can you tell me either through pure Java ... on windows how is it possible to find out the location of javaw.exe?

E.G.

import java.io.File;  class JavawLocation {      public static void main(String[] args) {         String javaHome = System.getProperty("java.home");         File f = new File(javaHome);         f = new File(f, "bin");         f = new File(f, "javaw.exe");         System.out.println(f + "    exists: " + f.exists());     } } 

Output

C:\Program Files (x86)\Java\jdk1.6.0_29\jre\bin\javaw.exe    exists: true Press any key to continue . . . 

And yes, I am confident that will work in a JRE.

like image 94
Andrew Thompson Avatar answered Sep 21 '22 13:09

Andrew Thompson