Java File Find findExecutableInPath(String exec)

Here you can find the source of findExecutableInPath(String exec)

Description

find Executable In Path

License

Apache License

Declaration

public static File findExecutableInPath(String exec) 

Method Source Code


//package com.java2s;
/*/*from   www. jav  a 2  s  .co m*/
 * Copyright 2013 OW2 Nanoko Project
 * 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.
 */

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static File findExecutableInPath(String exec) {
        // Build candidates
        List<String> candidates = new ArrayList<String>();
        candidates.add(exec);
        // Windows:
        candidates.add(exec + ".exe");
        candidates.add(exec + ".bat");
        candidates.add(exec + ".cmd");
        // Linux / Unix / MacOsX
        candidates.add(exec + ".sh");
        candidates.add(exec + ".bash");

        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) {
            for (String candidate : candidates) {
                File file = new File(pathDir, candidate);
                if (file.isFile()) {
                    return file;
                }
            }
        }

        // Search not successful.
        return null;
    }
}

Related

  1. findByFileName(List files, String fileName)
  2. findExe(String exeName, String... path)
  3. findExecutable(File baseLocation)
  4. findExecutable(String executableName)
  5. findExecutableInDirectory(String executable, File directory)
  6. findExecutableInSystemPath(String executable)
  7. findExecutableLocation(String executableName)
  8. findExecutableOnPath(String executable)
  9. findExecutableOnPath(String executableName)