wolfcastle
wolfcastle

Reputation: 5930

How to implement a custom local DNS service in Java?

I have a Java desktop application (deployed on Windows) that needs to know about a bunch of different hostnames/IPs. I prefer to use hostnames in the software, as it is more descriptive/less fragile than putting in IP addresses. However, this implies that the Windows machines need knowledge of this mapping.

Both the desktop application and the server software they are talking to are deployed inside a private network. There is no existing DNS service on the network that I am able to take advantage of.

The server software the desktops are talking to already have a hosts file defined with all of this information. In the past, I have included this hosts file with my desktop installer, and basically clobbered the Windows machine's host file. That's pretty ugly and I'd rather not do that again. I was hoping I could basically deploy my hosts file inside my desktop application, and run some kind of name service inside the application that is populated with the hosts file information. That way I don't affect other applications, or mess with the desktop's host file.

I found this article that describes implementing your own NameService class. This is the kind of solution I was looking for, however, it uses internal Sun classes (which I can get away with here, but I'd rather not have to). Also, the article is quite old (2006). In the Java 7 API, the network properties the article mentions are no longer listed. Is there a better/more modern way to do this?

Upvotes: 2

Views: 2364

Answers (2)

Sandman4
Sandman4

Reputation: 2741

I don't understand the complexity, replacing NameServer classes etc.. If all you need is a couple of hostname-ip mapping, have an array with hostanames and IPs and lookup your names in that array. Or have a file formatted like hosts, put the file in your application folder and populate the aforementioned array from that file.

The good solution would be to have a local DNS server though, like DNSMASQ or BIND.

Upvotes: 0

dash1e
dash1e

Reputation: 7807

Try with a project like JDNSS.

However if you search on the web you can find many links about this topic.

Upvotes: 1

Related Questions