3

Is it possible to get the file's path referred by a shell command in Java?

For example, when I type php -v in Windows command prompt, it would know that I am referring to C:\php\php.exe (for my own computer) because I added that to the system Path variable. Is it possible to do the same in Java?

I know that you can get the Path environment variable from Java and parse it using String.split(";"), but I wonder if there is a more direct way?

SOFe
  • 7,867
  • 4
  • 33
  • 61
  • I don't advice to do so but you can call native commands like `which` on linux or `where.exe` on Windows. IMHO, parsing the `PATH` variable is a cleaner option. – Arnaud Denoyelle Dec 11 '15 at 10:28
  • Perhaps by using `jmx` ? The beans might expose that, but this is just an idea. Scanning the path is not a good idea because although you have one php on the path another could be called directly. – Marged Dec 11 '15 at 10:32
  • This question has been answered [here](http://stackoverflow.com/questions/9006127/find-absolute-java-exe-path-programatically-from-java-code), [here](http://stackoverflow.com/questions/19369769/find-the-java-exe-location-from-java) and [here](http://stackoverflow.com/questions/4531626/get-path-to-java-jre). I may have missed some. You might want to look at [this answer](http://stackoverflow.com/a/28439873/1476989) for a windows-only solution – Peter Gordon Dec 11 '15 at 20:39
  • 1
    @pgmann I am talking about the system `$PATH` environment variable, not the java home. So, basically I want to get an output like @Amaud stated (`where`/`which`) programmatically without calling the command. I didn't know about that command before though. – SOFe Dec 12 '15 at 13:05

2 Answers2

2

Take a look at the below code from the JGit lib http://download.eclipse.org/jgit/site/3.7.1.201504261725-r/apidocs/org/eclipse/jgit/util/FS.html#searchPath(java.lang.String,%20java.lang.String...)

You can implement something similar

/**
 * Searches the given path to see if it contains one of the given files.
 * Returns the first it finds. Returns null if not found or if path is null.
 *
 * @param path
 *            List of paths to search separated by File.pathSeparator
 * @param lookFor
 *            Files to search for in the given path
 * @return the first match found, or null
 * @since 3.0
 **/
protected static File searchPath(final String path, final String... lookFor) {
    if (path == null)
        return null;

    for (final String p : path.split(File.pathSeparator)) {
        for (String command : lookFor) {
            final File e = new File(p, command);
            if (e.isFile())
                return e.getAbsoluteFile();
        }
    }
    return null;
}
Yogesh_D
  • 17,656
  • 10
  • 41
  • 55
1

You might be able to do something with this extract from my Java Command Prompt.

String cmd = "which "+<insert executable to find>; // linux
String cmd = "where "+<insert executable to find>; // windows

Process p = Runtime.getRuntime().exec(cmd);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
    ref.log.setText(ref.log.getText()+s+"\n");
    ref.updateDisplay();
}
// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
    ref.log.setText(ref.log.getText()+s+"\n");
    ref.updateDisplay();
}

The output should contain the path to the file.

Peter Gordon
  • 1,075
  • 1
  • 18
  • 38