17

I can print the path using System.getenv("PATH"). Then I probably can traverse those paths and use File to check if the file is exist.

Is there any faster way in Java ?

w00d
  • 5,416
  • 12
  • 53
  • 85
  • 1
    For what it's worth, I don't think there's a better way than what you propose – Miquel Mar 20 '13 at 13:00
  • @Nix is that question in SCALA ? I'm asking in JAVA dude – w00d Mar 20 '13 at 13:03
  • @w00d did you see any similarities between the answer below, and the answer for that question ? This link is also on the same subject http://stackoverflow.com/questions/2439984/how-to-check-if-a-program-is-installed-on-system . Take some time and research yourself `dude.` – Nix Mar 20 '13 at 13:04
  • JAVA has a huge library, and I wonder if some libraries have the support for this. Your 2nd link has answer for specific Windows Mozilla, not work for me. – w00d Mar 20 '13 at 13:05
  • @Nix I don't know what are you arguing about? I have given my reasoning that it's not a duplication. I had also given my solution, here I am asking for a better way. – w00d Mar 20 '13 at 13:12
  • The answer in the link proposed by @Nix is useful for Java too. – madth3 Mar 20 '13 at 15:07
  • 2
    This is not necessarily a duplicate. There are some alternative answers below that are actually better than the one that this thread is supposedly a duplicate of. Once again the SO post censors are at it... – Boltimuss Jul 14 '14 at 17:36
  • you can use the where command under windows. cmd /c where appExec. Then on the returned trimmed string you can do: if(shellResult!=null && shellResult.endsWith('git-bash.exe')) ..... bla bla – Marian Jun 25 '21 at 11:17

3 Answers3

5

You can use Runtime.getRuntime().exec("command"); in try ... catch section. If app won't be in PATH you will get an exception.

[EDIT]

But .. this will execute app instead of check. Sorry.

pepuch
  • 6,346
  • 7
  • 51
  • 84
  • 4
    won't this have the side affect of actually executing the command? – Colin D Mar 20 '13 at 13:01
  • you can use the where command under windows. cmd /c where appExec. Then on the returned trimmed string you can do: if(shellResult!=null && shellResult.endsWith('git-bash.exe')) ..... bla bla – Marian Jun 25 '21 at 11:18
4

Is there any faster way in Java ?

In terms of performance, no.

In terms of coding effort, probably no. Certainly I'm not aware of any 3rd-party Java library that will search the command search path to see if an executable exists.

Unfortunately searching for an executable on Windows is a little tricky because you have to account for the various types of executable ... based on file suffixes. Even on Linux / Unix you need to use the new Java 7 file attributes APIs to determine if a candidate file has the execute permissions set.

(I am aware that some commands can be run in ways that are harmless; e.g. they may support an option / argument that outputs a version string, or some help info. However, that only works in specific cases. I'm also aware that on Unix / Linux, there is a built-in shell command called "whereis" that can tell you if an executable command with a given name exists on the search path.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • you can use the where command under windows. cmd /c where appExec. Then on the returned trimmed string you can do: if(shellResult!=null && shellResult.endsWith('git-bash.exe')) ..... bla bla – Marian Jun 25 '21 at 11:18
3

Your approach will work.

I suggest using the File.listFiles(FileFilter filter) method on each directory in the path. This will make searching each directory simpler.

Colin D
  • 5,641
  • 1
  • 23
  • 35