Attempts to detect if an executable is present on the platform's PATH by name. On Windows, this is done through the where command. On Unix-like systems, the which command. @param imageName the executable name. @return true if so, false if not. @throws NullPointerException i
(String imageName)
| 378 | * @throws UnsupportedOperationException if a mechanism to search the PATH environment does not exist. |
| 379 | */ |
| 380 | public static boolean onPath(String imageName) |
| 381 | { |
| 382 | Objects.requireNonNull(imageName); |
| 383 | |
| 384 | try { |
| 385 | ProcessBuilder pb = (new ProcessBuilder()) |
| 386 | .redirectInput(Redirect.from(NULL_FILE)) |
| 387 | .redirectError(Redirect.appendTo(NULL_FILE)) |
| 388 | .redirectOutput(Redirect.appendTo(NULL_FILE)) |
| 389 | ; |
| 390 | if (IS_WINDOWS) |
| 391 | pb.command("where", imageName); |
| 392 | else if (IS_OSX || IS_LINUX) |
| 393 | pb.command("which", imageName); |
| 394 | else |
| 395 | throw new UnsupportedOperationException("OS does not have \"where\" or \"which\" available for PATH search!"); |
| 396 | int status = pb.start().waitFor(); |
| 397 | return status == 0; |
| 398 | } catch (IOException e) { |
| 399 | throw new UnsupportedOperationException("OS does not have \"where\" or \"which\" available for PATH search!"); |
| 400 | } catch (InterruptedException e) { |
| 401 | return false; // Shouldn't happen. |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | } |
no test coverage detected