• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting Operating System's User Session ID

 
Erik Deveza
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Tim Sparg
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Adrian Jones Uk
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan - notice that Adrian posted to an old thread, so you may not get an answer to your question.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic