Java File Attribute checkIfExecutableIsInPATH(final String executableName)

Here you can find the source of checkIfExecutableIsInPATH(final String executableName)

Description

Check if an executable is in the PATH.

License

LGPL

Parameter

Parameter Description
executableName the name of the executable

Return

true if an executable is in the PATH

Declaration

public static boolean checkIfExecutableIsInPATH(final String executableName) 

Method Source Code

//package com.java2s;
/*/*  w w  w. ja v a 2 s.  c om*/
 *                  Eoulsan development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public License version 2.1 or
 * later and CeCILL-C. This should be distributed with the code.
 * If you do not have a copy, see:
 *
 *      http://www.gnu.org/licenses/lgpl-2.1.txt
 *      http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.txt
 *
 * Copyright for this code is held jointly by the Genomic platform
 * of the Institut de Biologie de l'?cole normale sup?rieure and
 * the individual authors. These should be listed in @author doc
 * comments.
 *
 * For more information on the Eoulsan project and its aims,
 * or to join the Eoulsan Google group, visit the home page
 * at:
 *
 *      http://outils.genomique.biologie.ens.fr/eoulsan
 *
 */

import java.io.File;

import java.io.FilenameFilter;

public class Main {
    /**
     * Check if an executable is in the PATH.
     * @param executableName the name of the executable
     * @return true if an executable is in the PATH
     */
    public static boolean checkIfExecutableIsInPATH(final String executableName) {

        if (executableName == null) {
            throw new NullPointerException("executableName argument cannot be null");
        }

        final String pathEnv = System.getenv("PATH");

        if (pathEnv == null) {
            return false;
        }

        final FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(final File dir, final String name) {

                return executableName.equals(name);
            }
        };

        final String[] paths = pathEnv.split(":");

        for (String path : paths) {

            path = path.trim();

            if (path.isEmpty()) {
                continue;
            }

            File f = new File(path);

            if (!f.exists()) {
                continue;
            }

            if (f.isFile()) {

                if (executableName.equals(f.getName())) {
                    return true;
                }
            } else {

                final File[] files = f.listFiles(filter);

                if (files != null && files.length > 0) {
                    return true;
                }
            }
        }

        return false;
    }
}

Related

  1. doesExecutableExist(String executablePath)
  2. ensureExecutable(final File file)
  3. ensureExecutable(IPath path)
  4. ensureFileIsExecutable(String filename)