Java Path File Name nio findExecutablePaths(String executableName)

Here you can find the source of findExecutablePaths(String executableName)

Description

Locates an executable in the current system.

License

Open Source License

Parameter

Parameter Description
executableName The name of the executable to locate.

Exception

Parameter Description
NullPointerException If no executable name is supplied.
IllegalArgumentException If the supplied executable name is empty.

Return

A list with all the paths found for executables with the name supplied.

Declaration

public static List<Path> findExecutablePaths(String executableName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;

public class Main {
    /**//  w  w w .j a v a  2  s  . c  om
     * Locates an executable in the current system.
     * <br><br>
     * This method uses OS-specific commands to retrieve the path to the executable with the name supplied. On Windows
     * this means using the {@code where} command, on other systems the {@code which} command is used.
     * <br><br>
     * This is equivalent to calling {@link #findExecutablePaths(String, int)} with {@link Integer#MAX_VALUE} as an
     * argument.
     *
     * @param executableName The name of the executable to locate.
     *
     * @return A list with all the paths found for executables with the name supplied.
     *
     * @throws NullPointerException If no executable name is supplied.
     * @throws IllegalArgumentException If the supplied executable name is empty.
     *
     * @see <a href="http://stackoverflow.com/a/38073998">Source StackOverflow answer</a>
     * @see #findExecutablePath(String)
     * @see #findExecutablePaths(String, int)
     * @see #existsExecutableName(String)
     */
    public static List<Path> findExecutablePaths(String executableName) {
        return findExecutablePaths(executableName, Integer.MAX_VALUE);
    }

    /**
     * Locates an executable in the current system.
     * <br><br>
     * This method uses OS-specific commands to retrieve the path to the executable with the name supplied. On Windows
     * this means using the {@code where} command, on other systems the {@code which} command is used.
     *
     * @param executableName The name of the executable to locate.
     * @param maxResults The maximum number of results to retrieve.
     *
     * @return A list with all the paths found for executables with the name supplied, capped to the maximum number of
     *         results specified.
     *
     * @throws NullPointerException If no executable name is supplied.
     * @throws IllegalArgumentException If the supplied executable name is empty or if the maximum results is negative
     *                                  or zero.
     *
     * @see <a href="http://stackoverflow.com/a/38073998">Source StackOverflow answer</a>
     * @see #findExecutablePath(String)
     * @see #findExecutablePaths(String)
     * @see #existsExecutableName(String)
     */
    public static List<Path> findExecutablePaths(String executableName, int maxResults) {
        if (executableName == null)
            throw new NullPointerException();
        if (executableName.length() == 0 || maxResults <= 0)
            throw new IllegalArgumentException();

        List<Path> executablePaths = new LinkedList<>();
        try {
            Process process = new ProcessBuilder(isWindowsOS() ? "where" : "which", executableName).start();
            if (process.waitFor() == 0) {

                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                    String executablePath;
                    while (executablePaths.size() < maxResults && (executablePath = reader.readLine()) != null) {
                        executablePaths.add(Paths.get(executablePath));
                    }
                }
            }
        } catch (IOException | InterruptedException ignore) {
            // Empty catch block - Silently ignore errors and return current results
        }

        return executablePaths;
    }

    private static boolean isWindowsOS() {
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }
}

Related

  1. detectFilePath(String propertyName, String confFileName)
  2. endsWith(File file, String pathname)
  3. fileName(final Path path)
  4. fileName(final Path thePath)
  5. filterJarContainingClass(Collection jarsPaths, String className)
  6. findFile(Path directoryPath, String fileName)
  7. findFile(String basePath, final String fileName)
  8. findNextFileName(Path folder, String name)
  9. findRootPathForResource(String resourceName, ClassLoader classLoader)