Java File Attribute findInPath(String executable, String path, String pathSeparator)

Here you can find the source of findInPath(String executable, String path, String pathSeparator)

Description

Works kind of like the "which" program; the method won't try to apply PATHEXT to executable when searching the PATH.

License

Open Source License

Parameter

Parameter Description
executable The full name of the program to find in the path .
path The value of the PATH environment variable.
pathSeparator The path separation character for the agent's OS.

Return

The absolute path to the if it was found in the path; the original value of otherwise.

Declaration

public static String findInPath(String executable, String path, String pathSeparator) 

Method Source Code


//package com.java2s;
import java.io.File;

public class Main {
    /**/*from   w ww .  j  av a 2s . co m*/
     * Works kind of like the "which" program; the method won't try to apply
     * PATHEXT to {@link executable} when searching the PATH.
     * @param executable The full name of the program to find in the {@link path}.
     * @param path The value of the PATH environment variable.
     * @param pathSeparator The path separation character for the agent's OS. 
     * @return The absolute path to the {@link executable} if it was found in the path;
     *         the original value of {@link executable} otherwise.  
     */
    public static String findInPath(String executable, String path, String pathSeparator) {
        final String[] parts = path.split(pathSeparator);
        for (String part : parts) {
            final File potentialExecutable = new File(part, executable);
            if (potentialExecutable.exists()) {
                executable = potentialExecutable.getAbsolutePath();
                break;
            }
        }
        return executable;
    }
}

Related

  1. doesExecutableExist(String executablePath)
  2. ensureExecutable(final File file)
  3. ensureExecutable(IPath path)
  4. ensureFileIsExecutable(String filename)
  5. fileExecute(String path)
  6. findJavaCompilerExecutableInDir(File dir)
  7. findJavaExecutable(File vmInstallLocation)
  8. findUnixExecutable(String unixCommandLineExecutables)
  9. javaExecutable(File jre)