wood burning stoves 2.0*
The moose likes Java in General and the fly likes Getting Operating System's User Session ID Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting Operating System Watch "Getting Operating System New topic
Author

Getting Operating System's User Session ID

Erik Deveza
Greenhorn

Joined: Feb 21, 2011
Posts: 15
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Erik Deveza
Greenhorn

Joined: Feb 21, 2011
Posts: 15
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

Joined: Oct 27, 2005
Posts: 19723
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Erik Deveza
Greenhorn

Joined: Feb 21, 2011
Posts: 15
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

Joined: Oct 27, 2005
Posts: 19723
    
  20

You're welcome.
Tim Sparg
Ranch Hand

Joined: Dec 29, 2006
Posts: 40
for the username



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


There is no insanity so devastating in man's life as utter sanity
Erik Deveza
Greenhorn

Joined: Feb 21, 2011
Posts: 15
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

Joined: Feb 21, 2011
Posts: 15
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

Joined: Aug 29, 2012
Posts: 1
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

Joined: May 22, 2012
Posts: 867
    
    5
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

Joined: May 30, 2012
Posts: 679
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
 
subject: Getting Operating System's User Session ID