aspose file tools*
The moose likes Java in General and the fly likes Registry Values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Registry Values" Watch "Registry Values" New topic

Registry Values

S bitz
Ranch Hand

Joined: Jan 20, 2006
Posts: 80
Is there a way in java to get the Logon User Name in the registry for windows authentication ?

Henry Wong

Joined: Sep 28, 2004
Posts: 18971

The recommended way (if you can call it that) is to use JNDI. Unfortunately, you will also need to use a service provider that will connect to the windows registry. You will need to do a search on the Sun website to find a source.

If this "registry" is the active directory, then you could probably use the base JNDI libs, as active directory is compatible with LDAP.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

Trying to use APIs to access the registry in windows makes your code non portable. If you are sure that your application will not be ported to other platforms, use a native library to read these values and use JNI to invoke this library.

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
S bitz
Ranch Hand

Joined: Jan 20, 2006
Posts: 80
Hi John,
My application will not be ported to any other OS.
What is the native library you have mentioned? Could you please give me some more idea as to where can i get them or how i can create them?

Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Native library means to develope an application in C which accesses the registry and then use JNI to use that app.

Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

Here is the native function that seems to be the one you want (I am not entirely sure).

Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" ( _
ByVal hKey As Long, _ ' Handle to open registry key
ByVal lpValueName As String, _ ' Name of value
ByVal lpReserved As Long, _ ' Reserved, must be 0
lpType As Long, _ ' Registry data type constant
lpData As Any, _ ' By reference variable for data
lpcbData As Long _ ' Number of bytes of data written
) As Long

You can use java's JNI to invoke functions that are native to that platform, using a language like C.
Jan Cumps

Joined: Dec 20, 2006
Posts: 2510

A working example that uses the Windows reg command to read registry settings can be found on Real's.

It calls the reg query ... command to extract values from registry.
No JNI required.
It's not portable, but that seems to be no issue for you.

Don't know where the current user's name for nt authentication is stored.

Regards, Jan

OCUP UML fundamental and ITIL foundation
youtube channel
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

returns logon user name and it's portable

Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
I agree. Here's the link:
subject: Registry Values