sureshd
sureshd

Reputation: 565

Error while executing linux command using Java

I'm trying to execute the following command using Java

Process acquirInterfaces = Runtime.getRuntime().exec("dumpcap -D");

and getting error as

java.io.IOException: Cannot run program "dumpcap": java.io.IOException: error=2, No such file or directory

Linux box where im executing this command has got installed with dumpcap which is located under (/usr/local/bin)

What is the mistake im doing, please help

Upvotes: 1

Views: 833

Answers (3)

As noted, you need to use the full name of the executable. The reason for this is that in Unix systems, searching for commands on the path is handled by the shell, not the system call itself, so when you're running a command by using a system call, you have to specify exactly where to find the program to run.

Upvotes: 1

Luis Sep
Luis Sep

Reputation: 2402

Then try

Process acquirInterfaces = Runtime.getRuntime().exec("/usr/local/bin/dumpcap -D");

And in case you have any problem with the slash, use File.separator:

Process acquirInterfaces = Runtime.getRuntime().exec(File.separator + "usr" + File.separator + "local" + File.separator + "bin" + File.separator + "dumpcap -D");

Upvotes: 4

Gokul Nath KP
Gokul Nath KP

Reputation: 15553

Use the following line, with exact path:

Process acquirInterfaces = Runtime.getRuntime().exec("/usr/local/bin/dumpcap -D");

Upvotes: 4

Related Questions