Mat*_*ijs 8 java dns networking system
我一直在努力寻找运行我的Java应用程序的计算机的"描述".
我所追求的是在本地网络上宣传我的电脑时用于DNS的名称(下面屏幕截图中的"iMac Mattijs").
在Windows XP上,可以在此处找到此名称:控制面板 - >系统 - >计算机名称 - >计算机描述.

在Mac OS 10.6上,可以在此处找到此名称:系统首选项 - >共享 - >计算机名称
下面的方法没有提供我正在寻找的名称.看看这段代码:
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) {}
Run Code Online (Sandbox Code Playgroud)
在Windows上,这会打印:
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)
Run Code Online (Sandbox Code Playgroud)
在Mac上,我得到:
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
Run Code Online (Sandbox Code Playgroud)
但我正在寻找完整的字符串"iMac Mattijs".
任何线索都会非常受欢迎!
谢谢,Mattijs
Mac OS X 将计算机名称存储在系统配置动态存储中。其标准接口是通过系统配置框架实现的。执行此 API 的命令行工具是scutil:
$ scutil --get computerName
Hermes is awesome!
Run Code Online (Sandbox Code Playgroud)
(我暂时将计算机名称更改为带有空格和标点符号的名称,以便可以轻松地将其与主机名区分开来,在本例中主机名类似于hermes-is-awesome.local。)
您可以使用 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);
}
}
Run Code Online (Sandbox Code Playgroud)
不时javac FILE.java地javah SCDynamicStore。这会产生SCDynamicStore.h. 将其复制到SCDynamicStore.c并编辑为:
#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;
}
Run Code Online (Sandbox Code Playgroud)
(您可以通过使用 Obj-C 免费桥接并利用 来简化代码。在某些错误情况下,-[NSString UTF8String]可能需要抛出异常而不是仅返回。)NULL
然后您可以使用 来编译它clang -shared -I/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/JavaVM.framework/Headers/ -framework CoreFoundation -framework SystemConfiguration SCDynamicStore.c -o libSCDynamicStore.dylib。
现在,只要libSCDynamicStore.dylib是LD_LIBRARY_PATH在当前目录中,您就可以运行该应用程序:
$ java HostnameSC
computer name: Hermes is awesome!
Run Code Online (Sandbox Code Playgroud)