0

I am writing a java code to print current process that is on top of all process.

I write this:-

String process;
Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((process = input.readLine()) != null) {
    System.out.println(process);
}
input.close();

it prints all running processes but i want only one process that is on top of all other process and that is currently visible.

saksham agarwal
  • 250
  • 3
  • 14
  • What do you mean "on top of all other process"? Did you look at [Viewing the Process Tree](http://stackoverflow.com/questions/11269165/viewing-the-process-tree-tlist-tasklist)? You might need /SVC as the parameter, and then to parse the response. – KevinO Apr 02 '16 at 17:40
  • no.suppose if i am working on powerpoint then it print powerpoint process and not print all other processes.If i open crome then it print crome. – saksham agarwal Apr 02 '16 at 17:43
  • 1
    What does "currently visible" mean? Visible to whom? What is the meaning of "on top of"? Conceptually, processes are a tree. There are parents and children. And what does "working on powerpoint" mean? Do you mean you launched powerpoint from Java and want to find the process id? – KevinO Apr 02 '16 at 17:50
  • currently focused app? – Madhawa Priyashantha Apr 02 '16 at 17:52
  • just think of a situation. I first open browser and then open powerpoint (now i see powerpoint) then i switch back to browser(now i see browser on pc screen).so i wnat to check which process is currently visible by user. – saksham agarwal Apr 02 '16 at 17:54
  • 2
    First, you could see both powerpoint and the browser, so visible is not a correct description. I'm looking at 5 applications right now. @FastSnail did suggest the focused application, but an application may have multiple processes associated with it. Your terminology is very confusing. But for the question of the active application in Java for Windows, the answer is [Find out what application (window) is in focus in Java](http://stackoverflow.com/questions/5206633/find-out-what-application-window-is-in-focus-in-java). – KevinO Apr 02 '16 at 17:57

1 Answers1

2

You might replace code
while ((process = input.readLine()) != null) { System.out.println(process); }

with this,

process = input.readLine(); System.out.println(process);

N P
  • 23
  • 5