My goal is to print all internet connections on my computer. When I type netstat on cmd, I get a list of internet connections. I wanted to do the same in java, automatically.
My code is:
Runtime runtime = Runtime.getRuntime(); process = runtime.exec(pathToCmd); byte[] command1array = command1.getBytes();//writing netstat in an array of bytes OutputStream out = process.getOutputStream(); out.write(command1array); out.flush(); out.close(); readCmd(); //read and print cmd
But with this code, do I get C: \ eclipse \ workspace \ Tracker> Mais? instead of the netlist. Obviously, I am working with eclipse on Windows 7. What am I doing wrong? I looked in similar topics, but I did not find what was wrong. Thanks for answers.
EDIT:
public static void readCmd() throws IOException { is = process.getInputStream(); isr = new InputStreamReader(is); br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } }
source share