Java File Find findExecutableInSystemPath(String executable)

Here you can find the source of findExecutableInSystemPath(String executable)

Description

Tries to find the given executable (specified by its name) in the system's path.

License

Apache License

Parameter

Parameter Description
executable the name of the program to find, generally without the extension

Return

the file of the program to be searched for if found. null otherwise.

Declaration

public static File findExecutableInSystemPath(String executable) 

Method Source Code


//package com.java2s;
/*// ww  w  .  j  a v  a2s. com
 * #%L
 * Wisdom-Framework
 * %%
 * Copyright (C) 2013 - 2014 Wisdom Framework
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.File;

public class Main {
    /**
     * The set of extensions (including the empty extension) to append to the searched executable name.
     */
    public static final String[] EXECUTABLE_EXTENSIONS = new String[] {
            // The command itself (no extension)
            "",
            // Linux and Unix (scripts)
            ".sh", ".bash",
            // Windows
            ".exe", ".bat", ".cmd" };

    /**
     * Tries to find the given executable (specified by its name) in the system's path. It checks for a file having
     * one of the extensions contained in {@link #EXECUTABLE_EXTENSIONS}, and checks that this file is executable.
     *
     * @param executable the name of the program to find, generally without the extension
     * @return the file of the program to be searched for if found. {@code null} otherwise.
     */
    public static File findExecutableInSystemPath(String executable) {
        String systemPath = System.getenv("PATH");

        // Fast failure if we don't have the PATH defined.
        if (systemPath == null) {
            return null;
        }

        String[] pathDirs = systemPath.split(File.pathSeparator);

        for (String pathDir : pathDirs) {
            File dir = new File(pathDir);
            if (dir.isDirectory()) {
                File file = findExecutableInDirectory(executable, dir);
                if (file != null) {
                    return file;
                }
            }
        }
        // :-(
        return null;
    }

    /**
     * Tries to find the given executable (specified by its name) in the given directory. It checks for a file having
     * one of the extensions contained in {@link #EXECUTABLE_EXTENSIONS}, and checks that this file is executable.
     *
     * @param executable the name of the program to find, generally without the extension
     * @param directory  the directory in which the program is searched.
     * @return the file of the program to be searched for if found. {@code null} otherwise. If the given directory is
     * {@code null} or not a real directory, it also returns {@code null}.
     */
    public static File findExecutableInDirectory(String executable, File directory) {
        if (directory == null || !directory.isDirectory()) {
            // if the directory is null or not a directory => returning null.
            return null;
        }
        for (String extension : EXECUTABLE_EXTENSIONS) {
            File file = new File(directory, executable + extension);
            if (file.isFile() && file.canExecute()) {
                return file;
            }
        }
        // Not found.
        return null;
    }
}

Related

  1. findExe(String exeName, String... path)
  2. findExecutable(File baseLocation)
  3. findExecutable(String executableName)
  4. findExecutableInDirectory(String executable, File directory)
  5. findExecutableInPath(String exec)
  6. findExecutableLocation(String executableName)
  7. findExecutableOnPath(String executable)
  8. findExecutableOnPath(String executableName)
  9. findExecutableOnPath(String executableName)