How to find where javaw.exe is installed?

So, for the project I'm working on, I need to find out where javaw.exe is located on the user machine. How to do it? Assuming the user is on a Windows computer

The method I used is limited only to English versions of Windows.
I searched where the OS is installed, find the Program Files directory, find the Java directory, jdk , bin , then javaw.exe . I know this will not work on non-English versions of Windows.

What is a person-independent language?

+6
source share
5 answers

For completeness, let me mention that there are some places (on a Windows PC) to search for javaw.exe if it is not found in the path: (Still Reimeus recommendation should be your first attempt.)

1.
Java usually stores its location in the registry under the following key:
HKLM\Software\JavaSoft\Java Runtime Environement\<CurrentVersion>\JavaHome

2.
Newer versions of the JRE / JDK seem to also put a copy of javaw.exe in "C: \ Windows \ System32", so you might need to check as well (although there is a chance if it is, it will be found in the path also).

3.
Of course, there are "normal" installation locations:

  • 'C: \ Program Files \ Java \ jre * \ bin'
  • 'C: \ Program Files \ Java \ jdk * \ bin'
  • 'C: \ Program Files (x86) \ Java \ jre * \ bin'
  • 'C: \ Program Files (x86) \ Java \ jdk * \ bin'

[Please note that for older versions of Windows (XP, Vista (?)) This will only help on English versions of the OS. Fortunately, in a later version of Windows, "Program Files" will point to a directory regardless of its "display name" (which depends on the language).]

Some time ago, I wrote this piece of code to check javaw.exe in the above places. Maybe someone finds this useful:

 static protected String findJavaw() { Path pathToJavaw = null; Path temp; /* Check in Registry: HKLM\Software\JavaSoft\Java Runtime Environement\<CurrentVersion>\JavaHome */ String keyNode = "HKLM\\Software\\JavaSoft\\Java Runtime Environment"; List<String> output = new ArrayList<>(); executeCommand(new String[] {"REG", "QUERY", "\"" + keyNode + "\"", "/v", "CurrentVersion"}, output); Pattern pattern = Pattern.compile("\\s*CurrentVersion\\s+\\S+\\s+(.*)$"); for (String line : output) { Matcher matcher = pattern.matcher(line); if (matcher.find()) { keyNode += "\\" + matcher.group(1); List<String> output2 = new ArrayList<>(); executeCommand( new String[] {"REG", "QUERY", "\"" + keyNode + "\"", "/v", "JavaHome"}, output2); Pattern pattern2 = Pattern.compile("\\s*JavaHome\\s+\\S+\\s+(.*)$"); for (String line2 : output2) { Matcher matcher2 = pattern2.matcher(line2); if (matcher2.find()) { pathToJavaw = Paths.get(matcher2.group(1), "bin", "javaw.exe"); break; } } break; } } try { if (Files.exists(pathToJavaw)) { return pathToJavaw.toString(); } } catch (Exception ignored) {} /* Check in 'C:\Windows\System32' */ pathToJavaw = Paths.get("C:\\Windows\\System32\\javaw.exe"); try { if (Files.exists(pathToJavaw)) { return pathToJavaw.toString(); } } catch (Exception ignored) {} /* Check in 'C:\Program Files\Java\jre*' */ pathToJavaw = null; temp = Paths.get("C:\\Program Files\\Java"); if (Files.exists(temp)) { try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(temp, "jre*")) { for (Path path : dirStream) { temp = Paths.get(path.toString(), "bin", "javaw.exe"); if (Files.exists(temp)) { pathToJavaw = temp; // Don't "break", in order to find the latest JRE version } } if (pathToJavaw != null) { return pathToJavaw.toString(); } } catch (Exception ignored) {} } /* Check in 'C:\Program Files (x86)\Java\jre*' */ pathToJavaw = null; temp = Paths.get("C:\\Program Files (x86)\\Java"); if (Files.exists(temp)) { try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(temp, "jre*")) { for (Path path : dirStream) { temp = Paths.get(path.toString(), "bin", "javaw.exe"); if (Files.exists(temp)) { pathToJavaw = temp; // Don't "break", in order to find the latest JRE version } } if (pathToJavaw != null) { return pathToJavaw.toString(); } } catch (Exception ignored) {} } /* Check in 'C:\Program Files\Java\jdk*' */ pathToJavaw = null; temp = Paths.get("C:\\Program Files\\Java"); if (Files.exists(temp)) { try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(temp, "jdk*")) { for (Path path : dirStream) { temp = Paths.get(path.toString(), "jre", "bin", "javaw.exe"); if (Files.exists(temp)) { pathToJavaw = temp; // Don't "break", in order to find the latest JDK version } } if (pathToJavaw != null) { return pathToJavaw.toString(); } } catch (Exception ignored) {} } /* Check in 'C:\Program Files (x86)\Java\jdk*' */ pathToJavaw = null; temp = Paths.get("C:\\Program Files (x86)\\Java"); if (Files.exists(temp)) { try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(temp, "jdk*")) { for (Path path : dirStream) { temp = Paths.get(path.toString(), "jre", "bin", "javaw.exe"); if (Files.exists(temp)) { pathToJavaw = temp; // Don't "break", in order to find the latest JDK version } } if (pathToJavaw != null) { return pathToJavaw.toString(); } } catch (Exception ignored) {} } return "javaw.exe"; // Let just hope it is in the path :) } 
+8
source

try it

 for %i in (javaw.exe) do @echo. %~$PATH:i 
+3
source

To find "javaw.exe" in windows, I would use (using the package)

 for /f tokens^=2^ delims^=^" %%i in ('reg query HKEY_CLASSES_ROOT\jarfile\shell\open\command /ve') do set JAVAW_PATH=%%i 

It should work on Windows XP and Seven for JRE 1.6 and 1.7. Must grab the latest installed version.

+3
source

This worked with me:

  String javaHome = System.getProperty("java.home"); File f = new File(javaHome); f = new File(f, "bin"); f = new File(f, "javaw.exe"); //or f = new File(f, "javaws.exe"); //work too System.out.println(f + " exists: " + f.exists()); 
+1
source

Open cmd cmdlet

 cd \\ dir javaw.exe /s 
0
source

Source: https://habr.com/ru/post/949266/


All Articles