Saturday, June 4, 2016

How to get details from current machine in java using JNA(Java Native Access)

hello Folks,

[Java - Coding time]


  • How to get details from current machine in java.

More about JNA:

Java Native Access (JNA)

To use Windows and the domain controller for authentication, we can use native Windows APIs. To call those APIs in Java, we can use the Java Native Access (JNA) library, which you can find on GitHub at https://github.com/twall/jna and on Maven central:

[Libs]


[Source Code]


/**
 *
 */
package thiago.leoncio;

import java.net.UnknownHostException;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.Advapi32Util.Account;

/**
 * @author THiago
 * @Date 06/04/2016
 * @Description: How to get details from current machine.
 * @libs for JNA(jna.jar;jna-min.jar;jna-platform.jar)
 */
public class MachineDetails {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.getProperty("user.name"); //platform independent
java.net.InetAddress localMachine = null;
try {
localMachine = java.net.InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Hostname of local machine: " + localMachine.getHostName());
System.out.println("UserName logged on your machine:" +System.getProperty("user.name"));
System.out.println("=========== Java Native Access (JNA) details ===========");
for(Account account: Advapi32Util.getCurrentUserGroups()){
       System.out.println(account.fqn);
    }

}

}




[Output]:
Hostname of local machine: TLEONCIO_LAPTOP
UserName logged on your machine: tleo73638
=========== Java Native Access (JNA) details ===========
WST\domain users
Everyone
BUILTIN\Administrators
BUILTIN\Users
NT AUTHORITY\INTERACTIVE
CONSOLE LOGON
NT AUTHORITY\Authenticated Users
NT AUTHORITY\This Organization
..

Please, see this JNA and Waffle article also:  http://phonecoding.com/2016/10/how-to-use-waffle-library-for-jna-java.html



happy coding,
--Thiago

2 comments:

  1. can we login into the windows box through if we know username password through JNA

    ReplyDelete
  2. You can use Waffle to create a token on the client, send it to the server (e.g. over HTTP or whatever) and to validate that token on the server. At the end of this process (create, send and validate) you will know on the server who/what the client is.

    ReplyDelete