1

Need to run netstat -n | find ":3389" | find "ESTABLISHED" command using Java Runtime.

Tried like: Runtime.getRuntime().exec(cmd); but we can't do this if we have | (pipe) in our command.

I had found for linux command we can costruct like: String[] cmd = {"/bin/sh", "-c", "grep -c 'Report Process started' /path/to/server.log"}; Runtime.getRuntime().exec(cmd); but I need for windows, please let me know how we can do?

Kishore Reddy
  • 886
  • 3
  • 11
  • 19
  • 1
    same answer as http://stackoverflow.com/questions/15464111/run-cmd-commands-through-java despite I would prefer user1121883's answer – user85421 Mar 10 '17 at 07:12

2 Answers2

1

You should run only netstat -n and do the rest in java. You could also write a script, and execute the script instead of separate commands. If you really need to do everything in a single line that has pipes the command must be prefixed with cmd /C

Liviu Stirb
  • 5,876
  • 3
  • 35
  • 40
  • Ya that works, but checking is there any other way similar to linux. Because that way its looking much cleaner and we can find easily what we are doing. – Kishore Reddy Mar 10 '17 at 07:15
  • @KishoreReddy http://stackoverflow.com/questions/13046789/java-runtime-getruntime-execcmd-not-supporting-pipes – Liviu Stirb Mar 10 '17 at 07:17
0

In Windows we can achieve this like:

String[] cmd = { "cmd.exe", "/c",
                    "netstat -n | find \":3389\" | find \"ESTABLISHED\"" };
Process process = new ProcessBuilder(cmd).start();
Kishore Reddy
  • 886
  • 3
  • 11
  • 19