4

Is there a way to find a port opened by java process, given the process id of the process in java? Need to find it using java as it has to be platform independent

Given a process id : output any port/socket connections being used by that process.

Few things given: Process running in same jvm. There is only 1 port/socket being used by that Process, for which the Pid is given.

Can not do platform specific commands like ps -au | grep pid | ...

David Z
  • 128,184
  • 27
  • 255
  • 279
Devesh
  • 271
  • 1
  • 5
  • 10

3 Answers3

2

The answer is no. What processes have what ports is not information available to java applications at all. You'd need JNI and it would depend on the operating system.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
1

Have you tried jps? See http://download.oracle.com/javase/1.5.0/docs/tooldocs/share/jps.html

Would it help if you put the port number into the current thread name, then you could extract the port number from the thread name? e.g.

import java.net.ServerSocket;

public class SocketDriver { 
    public static void main(String[] args) throws Exception {       
        ServerSocket serverSocket = new ServerSocket(0);
        int localPort = serverSocket.getLocalPort();

        String threadName = Thread.currentThread().getName(); 
        Thread.currentThread().setName(threadName + ":" + localPort);
        System.out.println("port -> " + localPort);
        System.out.println("thread -> " + Thread.currentThread().getName());
        serverSocket.close();
    }
}

Output is:

port -> 51958
thread -> main:51958
  • thanks just tried it. quite close to what I needed. but jps shows only the explicitly set parameters. I have a port being opened dynamically using new ServerSocket(0). Need to figure out what is the port which got created. JPS falls short there. thanks for help. – Devesh Sep 02 '11 at 22:52
  • glad it helped, did you write the server? why do you need the port number? just curious –  Sep 02 '11 at 23:01
  • yes, it is needed in a server which starts at a port. The free port is discovered at run time using new ServerSocket(0) and it needs to be able to process JMX commands later (refresh , shutdown etc.). This is where the real port number is needed. The process is known by now. Any suggestions for finding the port number, given the process id? – Devesh Sep 03 '11 at 00:25
1

The answer is yes, depending on your operating system.

You need to find the appropriate command, for example on mac osx, it's lsof -i, then use Runtime to execute it and parse the output.

Here's some basic code that would do it:

 Process p = Runtime.getRuntime().exec(new String[] { "lsof", "-i" });
 InputStream commandOutput = p.getInputStream();
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Know of any util that captures all of the cross platform complexity under a nice API? – oligofren Jun 01 '21 at 10:48
  • @TheNimbleSurfer no, but there are basically only 2 OS flavours, so you could [determine the OS](https://stackoverflow.com/questions/228477/how-do-i-programmatically-determine-operating-system-in-java) then use either `lsof` or [the windows equivalent](https://stackoverflow.com/a/599268/256196) – Bohemian Jun 01 '21 at 22:28