Here you can find the source of findExecutableOnPath(String executableName)
public static File findExecutableOnPath(String executableName)
//package com.java2s; //License from project: Open Source License import java.io.File; public class Main { /**/*from ww w .j a v a 2s . c o m*/ * Tries to find the given executable on the path */ public static File findExecutableOnPath(String executableName) { //Check, did they specify something we can resolve? File fullyQualifiedExecutable = null; fullyQualifiedExecutable = new File(expandPath(executableName)); //if(1 > 0) // return new File(executableName); if (fullyQualifiedExecutable.isFile()) { return fullyQualifiedExecutable; } //Otherwise, keep looking String systemPath = System.getenv("PATH"); String[] pathDirs = systemPath.split(File.pathSeparator); for (String pathDir : pathDirs) { fullyQualifiedExecutable = new File(pathDir, executableName); if (fullyQualifiedExecutable.isFile()) { return fullyQualifiedExecutable; } } //We failed to find it return null; } /** * Tries to resolve any relative paths/symlinks on a given path */ public static String expandPath(String path) { if (System.getProperty("os.name").toLowerCase().indexOf("win") == 0) { String exp = path; try { Process shellExec = Runtime.getRuntime().exec("cmd.exe /C echo " + exp); java.io.BufferedReader reader = new java.io.BufferedReader( new java.io.InputStreamReader(shellExec.getInputStream())); String expandedPath = reader.readLine(); // Only return a new value if expansion worked. // We're reading from stdin. If there was a problem, it was written // to stderr and our result will be null. if (expandedPath != null) { exp = expandedPath; } } catch (java.io.IOException ex) { // Just consider it unexpandable and return original path. ex.printStackTrace(); System.out.println("WARNING: Failed to expand path '" + path + "'"); } File f = new File(exp); path = f.getAbsolutePath(); } else //We're probably on a posix system.... { try { String command = "ls -d " + path; Process shellExec = Runtime.getRuntime().exec(new String[] { "bash", "-c", command }); java.io.BufferedReader reader = new java.io.BufferedReader( new java.io.InputStreamReader(shellExec.getInputStream())); String expandedPath = reader.readLine(); // Only return a new value if expansion worked. // We're reading from stdin. If there was a problem, it was written // to stderr and our result will be null. if (expandedPath != null) { path = expandedPath; } } catch (java.io.IOException ex) { // Just consider it unexpandable and return original path. System.out.println( "WARNING: Failed to expand path '" + path + "' - probably due to a lack of unix..."); } } return path; } }