Fetching user name (windows) from a java application
Asked Answered
G

2

1

Problem:

I am logging into a virtual machine(RDC) using the below credentials:

The user is part of a domain group called as teldept

user:147852 pass:helloworld

when i try to get the user details from java application it gives me : 147852

but when i click on start menu at the top i can see my Name displayed.

How is this done? i want to access this name from java application

I use the below snippet:

System.getProperty("user.name");

Whatever the above snippet gives me is correct as aper oracle docs. I am logging in with ID: 147852 and above snippet gives me 14852 but some how in windows this ID:147852 is mapped with my name so only in the start menu in XP i am getting my name displyed instead of 147852. we need to know how this mapping is done between the ID & Name . I am guessing it has something to do with Domain or some network logic which i am not good with .

Gilmore answered 25/4, 2013 at 15:53 Comment(7)
How are you getting the "147852"? Post that snippet of code.Trigraph
@Trigraph i have edited the question. I hope i was not clear on my point.Gilmore
If you are running the java program from a command line - what does USERNAME env variable contain - try echo %USERNAME%Trigraph
@Trigraph thanks for your help . when i run the above suggested i am getting 147852Gilmore
Have you tried my answer below - using new com.sun.security.auth.module.NTSystem().getName(); what does that give?Trigraph
@Trigraph i got the same 147852 :-(Gilmore
XP Start menu shows FullName, not Logon ID - it cannot be got from Java without using JNI - updated my answer to say this.Trigraph
T
4

The name shown on XP's start menu is not the logon name. It's Full Name Corresponding to the Logon Name. Not sure if your login is a local login or a domain login. If it's a local login, go to Admin Tools -> Computer Management -> Users and Groups -> Here against your username (147852), you will find a full name. If your login is a domain login, you can similarly lookup your name in Active Directory - or search for it at other places.

This is very OS Specific and cannot be found by Java. You will need to do this using JNI and Windows API - Calling GetUserNameEx or NetUserGetInfo depending on type of user.

If you just want to get your logon name (147852), calling com.sun.security.auth.module.NTSystem().getName is a better way than using System.getProperty("user.name")

Trigraph answered 25/4, 2013 at 15:57 Comment(1)
But how do we get it if the username has spaces?Luminescent
S
2

From this SO question, you can use:

System.getProperty("user.name");

to return the currently logged in user. This will return the username string. I believe this is what you're asking for, but your question is rather unclear.

Stearic answered 25/4, 2013 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.