2

I am trying to run this line of code:

Process p = Runtime.getRuntime().exec(new String[] {"nmap -sP 192.168.1.0/24", g});

The above gives this error:

Exception in thread "main" java.io.IOException: Cannot run program "nmap -sP 192.168.1.0/24": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at Test.main(Test.java:14)

However, this line of code works fine:

Process p = Runtime.getRuntime().exec(new String[] {"nmap", g});

Here are some particulars:

  • Installed nmap 5.51 which works perfectly from the 'cmd line'.
  • Windows Vista.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
John R
  • 2,920
  • 13
  • 48
  • 62
  • possible duplicate of [How to execute command with parameters?](http://stackoverflow.com/questions/7134486/how-to-execute-command-with-parameters) – Raedwald Jan 07 '15 at 20:36

2 Answers2

3

You should be using this code:

Process p = Runtime.getRuntime().exec(new String[] {"nmap", "-sP", "192.168.1.0/24", g});

this is equivalent to:

"nmap -sP 192.168.1.0/24".split(" ");

The first entry in the array is always the file, and anything after that is parameters. What it was doing was looking for "nmap -sP 192.168.1.0/24" as a program, with no parameters.

xthexder
  • 1,555
  • 10
  • 22
1
 ProcessBuilder pb = new ProcessBuilder("nmap", "-sP", "192.168.1.0/24");
 Process p = pb.start();

Basically you have to separate the program from its arguments.

Reno
  • 33,594
  • 11
  • 89
  • 102