I am writing java application which gives me Port no. of application which is listening on particular port.
I want to get port no. of application which is listening on port 10001
Process p = Runtime.getRuntime().exec("lsof -i:10001 | awk '{print $2}'");
InputStream is=p.getInputStream();
byte b[]=new byte[is.available()];
is.read(b,0,b.length);
System.out.println(new String(b));
p.waitFor();
System.out.println("exit: " + p.exitValue());
p.destroy();
lsof -i:10001 | awk '{print $2}'
when i execute this in shell it gets me output
PID
8092
But in java application it gives me exit: 1
. Why doesnt it run in java ? Also can i get just port no only ? i.e. instead of PID 8091
i want 8092