Here you can find the source of findExecutableInDirectory(String executable, File directory)
Parameter | Description |
---|---|
executable | the name of the program to find, generally without the extension |
directory | the directory in which the program is searched. |
public static File findExecutableInDirectory(String executable, File directory)
//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; } }