How to get the full path of an executable in Java, if launched from Windows environment variable PATH?

There is no built-in function to do this. But you can find it the same way the shell finds executables on PATH.

Split the value of the PATH variable, iterate over the entries, which should be directories, and the first one that contains notepad.exe is the executable that was used.

public static String findExecutableOnPath(String name) {
    for (String dirname : System.getEnv("PATH").split(File.pathSeparator)) {
        File file = new File(dirname, name);
        if (file.isFile() && file.canExecute()) {
            return file.getAbsolutePath();
        }
    }
    throw new AssertionError("should have found the executable");
}

You can get the location of an executable in Windows by:

where <executable_name>

For example:

where mspaint returns:

C:\Windows\System32\mspaint.exe

And the following code:

Process process = Runtime.getRuntime().exec("where notepad.exe");
try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
    File exePath = new File(in.readLine());
    System.out.println(exePath.getParent());
}

Will output:

C:\Windows\System32