Kensuke Konishi
Kensuke Konishi

Reputation: 127

Want to get standard output or error when using Process

I want to use "which hoge" for obtaining a path for the hoge in Java code.

when "hoge" exists, the path can be obtained by Process#getInputStream.

but when "hoge" doesn't exist, both input stream and error stream from Process#get(Input|Error)Streams give "null".

but in terminal (OS X), if I try "which unexists", I get "unexists not found".

where does the message "unexists not found" go? How can I get this?

Does Java use another "which"?

Upvotes: 0

Views: 75

Answers (1)

Joni
Joni

Reputation: 111329

Your shell probably has "which" as a built-in command, so it doesn't use the /bin/which program. There are likely many differences between the two. You can check which which you use with the command "type which" and use the program instead of the built-in command using /bin/which.

From Java, you can know if a result was found by checking the process exit status returned by Process.waitFor(). If the status is 0 which found a match, if non-zero it didn't or there was some other error.

Upvotes: 1

Related Questions