Java File Find findExecutableOnPath(String executable)

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

Description

Checks the system path for the provided executable.

License

LGPL

Parameter

Parameter Description
executable a parameter

Declaration

public static String findExecutableOnPath(String executable) 

Method Source Code

//package com.java2s;
/*/*from ww  w  . jav  a 2  s  . co  m*/
 * Copyright (c) 2007-2012 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

import java.io.*;

public class Main {
    /**
     * Checks the system path for the provided executable.
     * If {@code executable} is a path (contains a path separator)
     * then it is returned unaltered
     *
     * @param executable
     * @return
     */
    public static String findExecutableOnPath(String executable) {
        if (executable.contains(File.separator))
            return executable;

        String systemPath = System.getenv("PATH");
        if (systemPath == null)
            systemPath = System.getenv("path");
        if (systemPath == null || File.pathSeparator == null)
            return executable;

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

        String fullPath = executable;
        for (String pathDir : pathDirs) {
            File file = new File(pathDir, executable);
            if (file.isFile()) {
                fullPath = file.getAbsolutePath();
                break;
            }
        }
        return fullPath;
    }

    /**
     * Returns an absolute path from the parent directory
     * of {@code referencePath} to the sub-element {@code inputPath}.
     * Safe to use with URLs.
     * If {@code inputPath} is an absolute path, it is returned unaltered.
     * <br/>
     * e.g.<br/>
     * String absPath = FileUtils.getAbsolutePath("test/mysession.xml", "/Users/bob/data/otherdata.xml");
     * System.out.println(absPath);<br/>
     * >>>> /Users/bob/data/test/mysession.xml
     *
     * @param inputPath     Relative path element
     * @param referencePath Absolute path root
     * @return
     */
    public static String getAbsolutePath(String inputPath, String referencePath) {
        if (isRemote(inputPath)) {
            return inputPath;
        }
        File inFile = new File(inputPath);
        if (inFile.isAbsolute()) {
            return inFile.getAbsolutePath();
        }

        String absolutePath;

        if (isRemote(referencePath)) {
            int idx = referencePath.lastIndexOf("/");
            String basePath = referencePath.substring(0, idx);
            absolutePath = basePath + "/" + inputPath;
        } else {
            File parent = new File(referencePath).getParentFile();
            File file = new File(parent, inputPath);
            absolutePath = file.getAbsolutePath();
        }
        return absolutePath;
    }

    public static boolean isRemote(String path) {
        if (path == null) {
            return false;
        }
        return path.startsWith("http://") || path.startsWith("https://") || path.startsWith("ftp://");
    }
}

Related

  1. findExecutable(String executableName)
  2. findExecutableInDirectory(String executable, File directory)
  3. findExecutableInPath(String exec)
  4. findExecutableInSystemPath(String executable)
  5. findExecutableLocation(String executableName)
  6. findExecutableOnPath(String executableName)
  7. findExecutableOnPath(String executableName)
  8. findJavaFiles(File dir, Collection filenames)
  9. findJavaFiles(File folder)