MCPcopy Create free account
hub / github.com/MTrop/DoomTools / onPath

Method onPath

src/main/java/net/mtrop/doom/tools/struct/util/OSUtils.java:380–403  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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}

Callers 3

checkGitMethod · 0.95
checkMercurialMethod · 0.95

Calls 1

startMethod · 0.65

Tested by

no test coverage detected