Java Runtimeでdigする

はじめに

Runtimeで外部アプリを叩いているだけじゃないか、というご指摘はごもっともです。

事前に準備する外部ライブラリ等はありません。

実装例

サンプルでは、動作確認しやすいようにmainメソッドで実行できるようにしてあります。 結果だけを確認したい場合は、この記事の一番下のリンク先で使えるようにしてありますのでご覧ください。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 *
 * @author tool-taro.com
 */
public class Dig {

    public static void main(String[] args) throws IOException, InterruptedException {

        //検索したいドメイン
        String source = "tool-taro.com";
        //nslookupのパス
        String nslookupPath = "/usr/bin/dig";

        //nslookup処理
        String[] commandArray = new String[3];
        commandArray[0] = nslookupPath;
        commandArray[1] = source;
        commandArray[2] = "ANY";
        final String encoding = "UTF-8";

        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        StringBuilder logBuilder = new StringBuilder();
        StringBuilder errorBuilder = new StringBuilder();
        int status = 0;

        try {
            process = runtime.exec(commandArray);
            final InputStream in = process.getInputStream();
            final InputStream ein = process.getErrorStream();

            Runnable inputStreamThread = () -> {
                BufferedReader reader = null;
                String line;
                try {
                    reader = new BufferedReader(new InputStreamReader(in, encoding));
                    while (true) {
                        line = reader.readLine();
                        if (line == null) {
                            break;
                        }
                        logBuilder.append(line).append("\n");
                    }
                }
                catch (Exception e) {
                }
                finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        }
                        catch (Exception e) {
                        }
                    }
                }
            };
            Runnable errorStreamThread = () -> {
                BufferedReader reader = null;
                String line;
                try {
                    reader = new BufferedReader(new InputStreamReader(ein, encoding));
                    while (true) {
                        line = reader.readLine();
                        if (line == null) {
                            break;
                        }
                        errorBuilder.append(line).append("\n");
                    }
                }
                catch (Exception e) {
                }
                finally {
                    if (reader != null) {
                        try {
                            reader.close();
                        }
                        catch (Exception e) {
                        }
                    }
                }
            };

            Thread inThread = new Thread(inputStreamThread);
            Thread errorThread = new Thread(errorStreamThread);

            inThread.start();
            errorThread.start();

            status = process.waitFor();
            inThread.join();
            errorThread.join();
        }
        finally {
            if (process != null) {
                try {
                    process.destroy();
                }
                catch (Exception e) {
                }
            }
        }
        //標準出力
        System.out.format("検索結果=%1$s", logBuilder.toString());
    }
}

動作確認

$ javac Dig.java
$ java Dig
$ 検索結果=; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.2 <<>> tool-taro.com ANY
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 36600
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1
...(省略)

環境

上記の実装をベースにWebツールも公開しています。 dig(ドメインサーチ)|Web便利ツール@ツールタロウ