1

i think the title explain the probleme, in fact, if i had a host and a port of running application how we can determine there name with java.

public String getApplicationName(String host,int port) {//some code} 
user3237201
  • 479
  • 1
  • 4
  • 13

2 Answers2

0

host and a port of running application

Only operation system is aware of applications that are running now. So it's impossible to get info about processes avoiding writing OS specific logic.

E.g for Windows it looks like:

Process proc = Runtime.getRuntime().exec ("tasklist.exe");
InputStream procOutput = proc.getInputStream ();
if (0 == proc.waitFor ()) {
    // TODO scan the procOutput for your data
} 
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
  • the application is running in the JVM, is it possible to get the main class at least ? – user3237201 Aug 22 '16 at 11:00
  • @user3237201, `jvm` is running inside the process managed by `OS`. 2 different applications run 2 different processes. It doesn't matter `jvm` or not processes are completely encapsulated, until you doesn't talk about `Threads` inside the same application. Please see http://stackoverflow.com/questions/200469/what-is-the-difference-between-a-process-and-a-thread – Rudziankoŭ Aug 22 '16 at 11:07
0

Sorry I jumped to conclusion in my comment earlier. I guess getting the process list from the command may be useful. Again OS specific...

try {
    String line;
    Process p = Runtime.getRuntime().exec("ps -e");
    BufferedReader input =
            new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line); //
//Run you pattern matcher here to parse data - host & port etc.
    }
    input.close();
} catch (Exception err) {
    err.printStackTrace();
}
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24