My goal is to print all the internet connections on my computer. When I type netstat
on CMD, I get the internet connections list. I wanted to do the same in Java, automatically.
My code:
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 I get C:\eclipse\workspace\Tracker>Mais?
instead of the list of connections. Obviously I'm working with Eclipse, in Windows 7. What am I doing wrong? I've looked in similar topics but I couldn't find what's wrong. Thank you for the 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);
}
}