• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting Operating System's User Session ID

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am currently trying to retrieve the session ID of my Windows user name through Java.

Manually I can do this by entering "query session" in MS command prompt. This shows the sessionname, username, id, state, type and device of all connected users including remote access sessions. The column id represents the session id. This is the exact data that I need in my Java application.

Is there an API in Java that I could use to retrieve that session id across operating systems?

Any input is highly appreciated.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Not all OSs will have such a thing. UNIX doesn't. There are vaguely related concepts on other OSs that you might decide to use -- process IDs of login shells, for example -- but nothing precisely analogous. I think you're going to have to do this yourself. As far as APIs -- using Runtime.exec() to run the "query session" command is about it.
 
Erik Deveza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:
Welcome to JavaRanch!



Thanks very much!

Ernest Friedman-Hill wrote:
As far as APIs -- using Runtime.exec() to run the "query session" command is about it.



Ok, then. Manual parsing it is.

Cheers!
 
Sheriff
Posts: 22789
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JNI is another option, but it's more complex since you will also need to figure out which native calls to use.
 
Erik Deveza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:JNI is another option, but it's more complex since you will also need to figure out which native calls to use.



JNI looks prettier and more stable. Thanks, Rob!
 
Rob Spoor
Sheriff
Posts: 22789
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the username



edit sorry just re-read that, you're looking for the session ID not userID
 
Erik Deveza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Sparg wrote:edit sorry just re-read that, you're looking for the session ID not userID


No worries. Thanks though for dropping your comment.

I successfully extracted the user session ID from Windows through JNA. However I am having no luck in getting the session ID from Linux. Does Java have Linux set of libraries to get this information?
 
Erik Deveza
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am guessing that there is no available library for this. So what I'll just do is to execute "echo $BASHPID" and manually parse the output in Java.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having the same issue with obtaining the Session ID using 'QUERY SESSION' command in Windows.

How did you do it via JNA?

Thanks
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Erik Deveza wrote:However I am having no luck in getting the session ID from Linux.



What is a "session ID" under Unix (including Linux) in the first place?
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ivan - notice that Adrian posted to an old thread, so you may not get an answer to your question.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic