0

How can we get the main title of the application from the processId of that application.

Can we use the ProcessHandler to get the window title?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
ameer zeya
  • 23
  • 4

1 Answers1

1

In Java 9 and later, you can use the ProcessHandle class to get information about a running process:

public class PH {
    public static void main(String[] args) {
        ProcessHandle.allProcesses().forEach(PH::info);
    }

    private static void info(final ProcessHandle processHandle) {
        processHandle.info().user().ifPresent(PH::print);
        processHandle.info().command().ifPresent(PH::print);
        processHandle.info().commandLine().ifPresent(PH::print);
        System.out.println();
    }

    private static void print(final String s) {
        System.out.print(String.format("%s\t", s));
    }
}

Approximate console output:

root    /usr/libexec/secd   /usr/libexec/secd   
root    /usr/libexec/trustd /usr/libexec/trustd --agent
user    /usr/libexec/lsd    /usr/libexec/lsd 

I'm not sure that you will be able to get the title of an application by this way, but you can check another methods of ProcessHandle.Info class.


Also you can try to use OS-specific utils for getting information about processes:

  • ps -e for Linux and Mac (you can read more about it here)
  • tasklist.exe for Windows (you can read more about it here)

In order to call that commands you can use the next code:

String command = "ps -e";
Process process = Runtime.getRuntime().exec(command);

// Get the input stream of the command's output
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;
while ((line = reader.readLine()) != null) {
    System.out.println(line);
}

Actually the similar question have been already asked, maybe you will find something useful here.


Example:

In order to get "window title" of all processes in OS Windows, you can run the next command:

tasklist /v /fo list |find /i "window title" |find /v "N/A"

It will print something like this:

...
Window Title: Untitled - Notepad
Window Title: Command Prompt
...

It means you can run that command using Runtime.getRuntime().exec(command) as I explained above:

String command = "tasklist /v /fo list |find /i \"window title\" |find /v \"N/A\"";
Process process = Runtime.getRuntime().exec(command);

// Get the input stream of the command's output
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line;
while ((line = reader.readLine()) != null) {
    System.out.println(line);
}
Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34
  • But this does not provides the window title. – ameer zeya Jan 25 '23 at 08:52
  • @ameerzeya what do you mean "window title"? What is it exactly? – Volodya Lombrozo Jan 25 '23 at 11:31
  • Example when we open notepad, the window title is shown as "Untitled - Notepad". If we have the processId of notepad, Can we get this String value of the title as "Untitled - Notepad"? @Volodya Lombrozo – ameer zeya Jan 25 '23 at 15:23
  • @ameerzeya I've added example, please have a look – Volodya Lombrozo Jan 25 '23 at 17:22
  • Thank you Volodya , this solution from tasklist works, I am using it in PowerShell as CMD little relatively slower. Do we have equivalent command in Linux or mac also, In Linux, I can see there is the command "ps" but it doesn't seems to have windowtitle as return parameter . @Volodya Lobrozo – ameer zeya Jan 26 '23 at 17:33
  • @ameerzeya I believe it would be better to investigate it in a separate question – Volodya Lombrozo Jan 26 '23 at 17:43