1

I am trying to use following command.

Runtime.getRuntime().exec("netsh -c interface dump > c:\\location1.txt");

But it produces no output.

I know we have to separate command and its arguments i tried but still failed.

I used following way.

Runtime.getRuntime().exec("netsh",new String[] "-c", "interface", "dump >", "c:\\location1.txt");

But still produces no output.

If anyone knows how to use all or some of the NETSH commands using runtime then it will be great helpful.

Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68

2 Answers2

1

You can try using ProcessBuilder

ProcessBuilder pb=new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process process=pb.start();
BufferedReader inStreamReader = new BufferedReader(
    new InputStreamReader(process.getInputStream())); 

while(inStreamReader.readLine() != null){
    //do something with commandline output.
}
A Paul
  • 8,113
  • 3
  • 31
  • 61
  • it throws exception here it is "Exception in thread "main" java.io.IOException: Cannot run program "netsh -c interface dump > C:\local.txt": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(Unknown Source) at com.kamani.proxyloader.ExecuteCommand.main(ExecuteCommand.java:16) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified" – Nirav Kamani Jan 09 '14 at 12:01
  • 2
    Should be like this `new ProcessBuilder("netsh.exe", "-c", "interface","dump");` No `> C:\local.txt` at all. – PeterMmm Jan 09 '14 at 12:08
  • ya i done it but i got output in console because i written System.out.println() in loop so now i have to manually write in file. – Nirav Kamani Jan 09 '14 at 12:24
1

This is netshon Windows ? Try to specifi full path to executable:

Runtime.getRuntime().exec("C:/full/path/netsh.exe",...
PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • it's still not working.All other command works but i don't know why above command is not working. – Nirav Kamani Jan 09 '14 at 08:13
  • You cannot redirect the output. The redirect `>` is part of a shell command. You may catch the `netsh`output as @A Paul said or start `cmd.exe` with a commandline. Look here: http://stackoverflow.com/questions/5928225/how-to-make-pipes-work-with-runtime-exec – PeterMmm Jan 09 '14 at 08:55
  • ya you are right but i am still unable to understand can you tell me how can i resolve this issue? – Nirav Kamani Jan 09 '14 at 12:02