dnsjava is an implementation of DNS in Java. It supports almost all defined record types (including the DNSSEC types), and unknown types. It can be used for queries, zone transfers, and dynamic updates. It includes a cache which can be used by clients, and an authoritative only server. It supports TSIG authenticated messages, partial DNSSEC verification, and EDNS0. It is fully thread safe.
dnsjava was started as an excuse to learn Java. It was useful for testing new features in BIND without rewriting the C resolver. It was then cleaned up and extended in order to be used as a testing framework for DNS interoperability testing. The high level API and caching resolver were added to make it useful to a wider audience. The authoritative only server was added as proof of concept.
dnsjava on Github
This repository has been a mirror of the dnsjava project at Sourceforge since 2014 to maintain the Maven build for publishing to Maven Central. As of 2019-05-15, Github is officially the new home of dnsjava.
Some settings of dnsjava can be configured via system properties:
|DNS server(s) to use for resolving. Comma separated list. Can be IPv4/IPv6 addresses or hostnames (which are resolved using Java's built in DNS support).|
|Comma separated list of DNS search paths.|
|Sets a threshold for the number of dots which must appear in a name given to resolve before an initial absolute query will be made.|
|Comma separated key-value pairs, see below.|
|Set to true to disable static ResolverConfig initialization.|
|Set to true to enable the reflection based DNS server lookup, see limitations below.|
|dnsjava.udp.ephemeral.start||Integer||49152 (Linux: 32768)||50000|
|First ephemeral port for UDP-based DNS queries.|
|dnsjava.udp.ephemeral.end||Integer||65535 (Linux: 60999)||60000|
|Last ephemeral port for UDP-based DNS queries.|
|Use an OS-assigned ephemeral port for UDP queries. Enabling this option is insecure! Do NOT use it.|
|Maximum number of CNAMEs to follow in a chain.|
|Use the system's hosts file for lookups before resorting to a resolver.|
The dnsjava.options configuration options can also be set programmatically
Options class. Please refer to the Javadoc for details.
|BINDTTL||Boolean||false||Print TTLs in BIND format|
|multiline||Boolean||false||Print records in multiline format|
|noPrintIN||Boolean||false||Do not print the class of a record if it is
|tsigfudge||Integer||300||Sets the default TSIG fudge value (in seconds)|
|sig0validity||Integer||300||Sets the default SIG(0) validity period (in seconds)|
dnsjava comes with several built-in resolvers:
SimpleResolver: a basic resolver that uses UDP by default and falls back to TCP if required.
ExtendedResolver: a resolver that uses multiple
SimpleResolvers to send the queries. Can be configured to query the servers in a round-robin order. Blacklists a server if it times out.
DohResolver: a proof-of-concept DNS over HTTP resolver, e.g. to use
The project dnssecjava has a resolver that validates responses with DNSSEC.
Migrating from version 2.1.x to v3
dnsjava 3 has significant API changes compared to version 2.1.x and is neither source nor binary compatible. The most important changes are:
- The minimum supported version is Java 8
- Uses slf4j for logging and thus needs
slf4j-apion the classpath
- The command line tools were moved to the
- On Windows, JNA should be on the classpath for the search path
ResolverAPI for custom resolvers has changed to use
CompletionStage<Message>for asynchronous resolving. The built-in resolvers are now fully non-blocking and do not start a thread per query anymore.
- Many methods return a
List<T>instead of an array. Ideally, use a for-each loop. If this isn't possible, call
size()instead of using
- RRset returns a List instead of an
Iterator. Ideally, modify your code to use a for-each loop. If this is not possible, create an iterator on the returned list:
- Methods using
java.util.Dateare deprecated. Use the new versions with
- The type hierarchy of
SMIMEARecordchanged, it now inherits from
TLSARecordand constants are shared
Records are no longer marked as
Serializable. Use the RFC defined serialization formats:
Replacing the standard Java DNS functionality
To load the dnsjava service provider, build dnsjava on JDK 8 and set the system property:
This instructs the JVM to use the dnsjava service provide for DNS at the highest priority.
mvn package from the toplevel directory to build dnsjava. JDK 8
or higher is required.
Matt Rutherford contributed a number of unit
tests, which are in the tests subdirectory. The hierarchy under tests
mirrors the org.xbill.DNS classes. To run the unit tests, execute
There's no standard way to determine what the local nameserver or DNS search path is at runtime from within the JVM. dnsjava attempts several methods until one succeeds.
- The properties
dns.search(comma delimited lists) are checked. The servers can either be IP addresses or hostnames (which are resolved using Java's built in DNS support).
- On Unix/Solaris,
- On Windows, if JNA is available
on the classpath, the
GetAdaptersAddressesAPI is used.
- On Android the
ConnectivityManageris used (requires initialization using
sun.net.dns.ResolverConfigurationclass is queried if enabled. As of Java 16 the JVM flag
--add-opens java.base/sun.net.dns=ALL-UNNAMEDis also required.
- If available and no servers have been found yet, JNDI-DNS is used.
- If still no servers have been found yet, use the fallback properties. This can be used to query e.g. a well-known public DNS server instead of localhost.
- As a last resort,
localhostis used as the nameserver, and the search path is empty.
dnsjava is placed under the BSD-3-Clause license.
- Brian Wellington (@bwelling), March 12, 2004
- Various contributors, see Changelog
- Ingo Bauersachs (@ibauersachs), current maintainer
- Thanks to Network Associates, Inc. for sponsoring some of the original dnsjava work in 1999-2000.
- Thanks to Nominum, Inc. for sponsoring some work on dnsjava from 2000 through 2017.