Title: commons-exec: Executing a program on the system PATH?
Tags: java;path;environment-variables;system-calls;apache-commons-exec
Question: I'm trying to execute a program (convert from ImageMagick, to be specific) whose parent folder exists on the path. Ergo, when I run convert from the command line, it runs the command. The following, however, fails:
String command = "convert"
CommandLine commandLine = CommandLine.parse(command);
commandLine.addArgument(...)
int exitValue = executor.execute(commandLine);
If I specify the full path of the convert executable (C:\Program files\...) then this code works. If I don't do this, I get an exception thrown with exit value 4.
How do I get commons-exec to recognize the system path?

Here is the accepted answer: I've run into issues like this before where the system set PATH is not what the java process is seeing.  As a way to debug this you can print out what the java process sees as the path env variable by using:
EnvironmentUtils.getProcEnvironment();
Which will give you a map and you can look to see if Java can see the path variable.  If it is not there then the next step would be to figure out why you can't see it.
If it is there I would try running your excutor.execute command as follows:
int exitValue = executor.execute(commandLine, EnvironmentUtils.getProcEnvironment());


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2010:00:04/22/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><path><environment-variables><system-calls><apache-commons-exec>', 'Title': 'commons-exec: Executing a program on the system PATH?', 'ViewCount': 3371, 'id': 2693020, 'user_AboutMe': '', 'user_AccountId': 29046.0, 'user_DisplayName': 'Stefan Kendall', 'user_Reputation': 64644.0, 'user_Views': 7499.0, 'user_id': 78182.0}}