Java library for finding executables -


i using the processbuilder class execute executables on windows , linux.

is there easy way find these executables without knowing directory path executable.

e.g.

//which command functionality string executable = which("executable_name");  list<string> command = new arraylist<string>(); command.add(executable); processbuilder builder = new processbuilder(command); .. .. 

it great if there function command on linux?

any ideas or have loop on , parse path environment variables using

system.getenv("path"); 

use where command on windows.

where [/r dir] [/q] [/f] [/t] pattern 

if not specify search directory using /r, searches current directory , in paths specified path environment variable. here's sample code finds 2 locations notepad.exe resides on windows.

string searchcmd; if (system.getproperty("os.name").contains("windows")) {     searchcmd = "where"; } else { // i'm assuming linux here     searchcmd = "which"; }  processbuilder procbuilder = new processbuilder(searchcmd, "notepad.exe"); process process = procbuilder.start();  arraylist<string> filepaths = new arraylist<string>(); scanner scanner = new scanner(process.getinputstream()); while (scanner.hasnextline()) {     filepaths.add(scanner.nextline()); } scanner.close();  system.out.println(filepaths); 

output:

[c:\windows\system32\notepad.exe, c:\windows\notepad.exe] 

note: i've tested on windows. may have modify (probably command options , way parse which output) make work on linux.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -