Java File Find findExecutableInDirectory(String executable, File directory)

Here you can find the source of findExecutableInDirectory(String executable, File directory)

Description

Tries to find the given executable (specified by its name) in the given directory.

License

Apache License

Parameter

Parameter Description
executable the name of the program to find, generally without the extension
directory the directory in which the program is searched.

Return

the file of the program to be searched for if found. null otherwise. If the given directory is null or not a real directory, it also returns null .

Declaration

public static File findExecutableInDirectory(String executable, File directory) 

Method Source Code


//package com.java2s;
/*//from w ww .  j a  va 2 s  .  c om
 * #%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 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. findByExtension(final File directory, final String extension)
  2. findByFileName(List files, String fileName)
  3. findExe(String exeName, String... path)
  4. findExecutable(File baseLocation)
  5. findExecutable(String executableName)
  6. findExecutableInPath(String exec)
  7. findExecutableInSystemPath(String executable)
  8. findExecutableLocation(String executableName)
  9. findExecutableOnPath(String executable)