Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Getting list of Operating System prosesses into Java Application

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to get all the currently executing processes into my java process. ( You can view those processes list if you go into task manager & selecing processes). But I need these list to be accessed from my java application . How can i do so...
 
Sheriff
Posts: 22586
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Shailesh S.",

Please read your private messages regarding an important announcement.

Thank you,

Rob



Now about your problem. There are two ways:
1) use JNI; you will need native Windows functions CreateToolhelp32Snapshot, Process32First, Process32Next and CloseHandle, plus native structure ProcessEntry.

2) execute tasklist.exe (located in C:\WINDOWS\system32) using ProcessBuilder / Runtime.exec and parse the results. Check out "tasklist /?" for more options. Especially the "/FO CSV" can be useful to make it a bit easier to parse.
 
Shailesh Sukare
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried second way. It worked correctly. But this is a solution for Windows platform. Now if I want list of Processes on Linux/Unix platform in that is there any way?
 
Rob Spoor
Sheriff
Posts: 22586
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Execute "ps" instead of tasklist.exe. "ps --help" or "man ps" can help you out.

Unfortunately, there is no platform independent way to do this.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.getProperty("os.name") will tell you which OS your're dealing with, so you can implement both ways and the choose the appropriate one at runtime.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic