Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find my "computer description" in a Java application on Windows and/or Mac?

I have been struggling to find the "description" of the computer on which my Java application is running.

What I'm after is the name used for DNS when advertising my computer on the local network ("iMac Mattijs" in the screen shots below).

On Windows XP, this name can be found here: Control Panel -> System -> Computer Name -> Computer Description.

alt text

On Mac OS 10.6, this name can be found here: System Preferences -> Sharing -> Computer Name

alt text

The methods below don't deliver the name I'm looking for. Have a look at this code:

    System.out.println("COMPUTERNAME environment variable: " + System.getenv("COMPUTERNAME"));
    try { System.out.println("localhost name: " + InetAddress.getLocalHost().getHostName()); } 
    catch (UnknownHostException e1) {}

    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        while (interfaces.hasMoreElements()) {
            NetworkInterface thisInterface = interfaces.nextElement();
            Enumeration<InetAddress> addresses = thisInterface.getInetAddresses();

            System.out.println("* network interface: " + thisInterface.getDisplayName());
             while (addresses.hasMoreElements()) {
                 InetAddress address = addresses.nextElement();
                 System.out.println(" - address: " + address.getCanonicalHostName());
             }
        }           
    } catch (SocketException e) {}

On Windows, this prints:

COMPUTERNAME environment variable: ARTTECH-51CA5F5
localhost name: arttech-51ca5f5
* network interface: MS TCP Loopback interface
 - address: localhost
* network interface: NVIDIA nForce Networking Controller - Packet Scheduler Miniport
* network interface: Broadcom 802.11n Network Adapter - Packet Scheduler Miniport
 - address: arttech-51ca5f5.lan
* network interface: Bluetooth Device (Personal Area Network)

On Mac, I get:

COMPUTERNAME environment variable: null
localhost name: imac-mattijs.lan 
* network interface: en1
 - address: imac-mattijs.lan
 - address: imac-mattijs.local
* network interface: lo0
 - address: localhost
 - address: fe80:0:0:0:0:0:0:1%1
 - address: localhost

But I am looking for the full String "iMac Mattijs".

Any clues would be very welcome!

Thanks, Mattijs

like image 724
Mattijs Avatar asked Oct 14 '10 14:10

Mattijs


2 Answers

Here is what i found upon some experiments. The Computer Description is stored in the registry key

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\LanmanServer\Parameters\srvcomment

So, If we have any API which we can use to get the reg key values, we can find it out.

Also, Found the following link which gives a good class to query reg key values:

http://www.rgagnon.com/javadetails/java-0630.html

And, Using the class WinRegistry given in the above site, I could successfully find out the Computer Description using the code:

String computerName = WinRegistry.readString(WinRegistry.HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\services\\LanmanServer\\Parameters", "srvcomment");
like image 113
Balaram Avatar answered Oct 09 '22 09:10

Balaram


Mac OS X stores the computer name in the System Configuration dynamic store. The standard interface to this is via the System Configuration framework. The commandline tool exercising this API is scutil:

$ scutil --get computerName
Hermes is awesome!

(I temporarily changed my computer name to something with spaces and punctuation so it would be readily distinguishable from the hostname, which in this case would be something like hermes-is-awesome.local.)

You can interface with this pretty easily using JNI:

class SCDynamicStore {
  public native String copyComputerName();
  static {
    System.loadLibrary("SCDynamicStore");
  }
}

class HostnameSC {
  public static void
  main(String[] args) {
    SCDynamicStore store = new SCDynamicStore();
    String computerName = store.copyComputerName();
    System.out.format("computer name: %s\n", computerName);
  }
}

Now javac FILE.java and then javah SCDynamicStore. This produces SCDynamicStore.h. Copy this to SCDynamicStore.c and edit it to read:

#include "SCDynamicStore.h"
#include <SystemConfiguration/SystemConfiguration.h>

JNIEXPORT jstring JNICALL
Java_SCDynamicStore_copyComputerName(JNIEnv *env, jobject o)
{
  SCDynamicStoreRef store = NULL;
  CFStringRef computerName = NULL;
  CFStringEncoding UTF8 = kCFStringEncodingUTF8;
  CFIndex length;
  Boolean ok;
  jstring computerNameString = NULL;
  CFStringRef process = CFSTR("com.me.jni.SCDynamicStore");

  store = SCDynamicStoreCreate(NULL, process, NULL/*callout*/, NULL/*ctx*/);
  if (!store) {
    fprintf(stderr, "failed to get store\n");
    goto CantCreateStore;
  }

  computerName = SCDynamicStoreCopyComputerName(store, NULL);
  if (!computerName) {
    fprintf(stderr, "failed to copy computer name\n");
    goto CantCopyName;
  }

  length = CFStringGetLength(computerName);
  length = CFStringGetMaximumSizeForEncoding(length, UTF8);
  {
    char utf8[length];
    if (!CFStringGetCString(computerName, utf8, sizeof(utf8), UTF8)) {
      fprintf(stderr, "failed to convert to utf8\n");
      goto CantConvert;
    }
    computerNameString = (*env)->NewStringUTF(env, utf8);
  }

CantConvert:
  CFRelease(computerName);
CantCopyName:
  CFRelease(store), store = NULL;
CantCreateStore:
  return computerNameString;
}

(You could simplify the code by using Obj-C toll-free bridging and taking advantage of -[NSString UTF8String]. It might be desirable to throw an exception instead of just returning NULL in some cases of error.)

You can then compile this using clang -shared -I/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/JavaVM.framework/Headers/ -framework CoreFoundation -framework SystemConfiguration SCDynamicStore.c -o libSCDynamicStore.dylib.

Now, provided libSCDynamicStore.dylib is along LD_LIBRARY_PATH, which it will be when it's in the current directory, you can run the application:

$ java HostnameSC
computer name: Hermes is awesome!
like image 32
Jeremy W. Sherman Avatar answered Oct 09 '22 10:10

Jeremy W. Sherman