域名解析在 Ubuntu64 9.04 计算机上的 Java 应用程序中不起作用。所有其他软件都能正确解析 DNS

2022-09-03 05:32:55

我在Ubuntu64 9.04 PC上安装了许多Java应用程序,它们都不能解析域名(也有多个JRE - 其中一些是IBM产品)。如果我将域名放在主机文件中,并带有其关联的IP地址,那么Java应用程序仅适用于这些域。所有其他非Java程序 - 如ping,firefox等 - 都可以很好地处理域分辨率。我试图在java.security文件中禁用DNS缓存 - 对于我所有的JRE - 但这也没有奏效。如果能帮助弄清楚这个问题,我将不胜感激。谢谢!


更新:我确定我的家中或办公室中没有代理服务器。- 我感谢你们在这里帮助我。我真的很想使用Linux而不是Windows,因为我又在做Java开发了。

jgreenwood@jeg-ubuntu64:~$ cat /etc/resolv.conf
# Generated by NetworkManager
domain hsd1.in.comcast.net.
search hsd1.in.comcast.net.
nameserver 192.168.0.1
jgreenwood@jeg-ubuntu64:~$ env | grep -i proxy
jgreenwood@jeg-ubuntu64:~$ dig google.com

; <<>> DiG 9.5.1-P2 <<>> google.com
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56845
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;google.com.            IN  A

;; ANSWER SECTION:
google.com.     58  IN  A   74.125.53.100
google.com.     58  IN  A   74.125.45.100
google.com.     58  IN  A   74.125.67.100

;; Query time: 35 msec
;; SERVER: 192.168.0.1#53(192.168.0.1)
;; WHEN: Thu Oct 22 13:37:26 2009
;; MSG SIZE  rcvd: 76

更新:我在RAD中编写了这个java程序:

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.PrintWriter;
import java.io.StringWriter;

public class DomainResolutionTest {

    public static void main(String[] args) {
        if (args.length == 0) args = new String[] { "www.google.com" };

        try {
            InetAddress ip = InetAddress.getByName(args[0]);
            System.out.println(ip.toString());
        }catch (UnknownHostException uhx) {
            System.out.println("ERROR: " + uhx.getMessage() + "\n" + getStackTrace(uhx));
            Throwable cause = uhx.getCause();
            if (cause != null) System.out.println("CAUSE: " + cause.getMessage());
        }

    }

    public static String getStackTrace(Throwable t)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        t.printStackTrace(pw);
        pw.flush();
        sw.flush();
        return sw.toString();
    }

}

输出为:

ERROR: www.google.com
java.net.UnknownHostException: www.google.com
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:862)
    at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1213)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1166)
    at java.net.InetAddress.getAllByName(InetAddress.java:1096)
    at java.net.InetAddress.getAllByName(InetAddress.java:1032)
    at java.net.InetAddress.getByName(InetAddress.java:982)
    at DomainResolutionTest.main(DomainResolutionTest.java:12)

从命令行:(相同的结果)

jgreenwood@jeg-ubuntu64:~$ javac DomainResolutionTest.java
jgreenwood@jeg-ubuntu64:~$ java DomainResolutionTest 
ERROR: www.google.com
java.net.UnknownHostException: www.google.com
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
    at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1200)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1153)
    at java.net.InetAddress.getAllByName(InetAddress.java:1083)
    at java.net.InetAddress.getAllByName(InetAddress.java:1019)
    at java.net.InetAddress.getByName(InetAddress.java:969)
    at DomainResolutionTest.main(DomainResolutionTest.java:12)
jgreenwood@jeg-ubuntu64:~$ java -version
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Server VM (build 14.2-b01, mixed mode)
jgreenwood@jeg-ubuntu64:~$ 

答案 1

再一次,感谢这里人们的指导,我找到了答案。当我执行以下操作时,上面的Java程序可以工作:

java -Djava.net.preferIPv4Stack=true DomainResolutionTest

详:

jgreenwood@jeg-ubuntu64:~$ java -Djava.net.preferIPv4Stack=true DomainResolutionTest 
www.google.com/209.85.225.106
jgreenwood@jeg-ubuntu64:~$ java DomainResolutionTest ERROR: www.google.com
java.net.UnknownHostException: www.google.com
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849)
    at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1200)
    at java.net.InetAddress.getAllByName0(InetAddress.java:1153)
    at java.net.InetAddress.getAllByName(InetAddress.java:1083)
    at java.net.InetAddress.getAllByName(InetAddress.java:1019)
    at java.net.InetAddress.getByName(InetAddress.java:969)
    at DomainResolutionTest.main(DomainResolutionTest.java:12)

事实证明,IPv6堆栈中存在一个错误。有几篇文章让我得出了这个结论:

http://uclue.com/?xq=2127

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=477211

https://answers.launchpad.net/ubuntu/+question/23024

我当然希望我不必在每次运行Java应用程序时都添加IPv4废话。最终的解决方案可能在第二个链接中 - 缺少一个包。我们拭目以待。

Debian Bug report logs - #477211 ia32-sun-java6-bin: 需要依赖于 lib32nss-mdns ...无法解析域名。解析在系统的其余部分很好,没有用其他JDK测试过。从软件包安装并使用更新 java 替代项进行设置。

DNS适用于我系统上的其他所有内容。在 Debian 2.6.23-AMD64 上运行。尝试了Lenny软件包和Sid软件包。与 sun-java6-bin 一起工作,在 ia32-sun-java6-bin 上失败。精 氨 酸。。。。如果你用strace打java,你会看到它正在尝试使用libnss_mdns4_minimal.so.2,它在lib32nss-mdns包中可用。您应该添加对它的依赖关系以修复错误。

同样的事情也发生在 sun-java6-bin - libnss-mdns 在这里使用。

我的机器上确实缺少该软件包:

jgreenwood@jeg-ubuntu64:~$ dpkg -L lib32nss-mdns
Package `lib32nss-mdns' is not installed.
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.

无论哪种方式,我希望这篇文章可以帮助别人,因为这是一个需要弄清楚的主要PITA。


答案 2

此解决方案()在长时间运行时也有效。-Djava.net.preferIPv4Stack=truelookupAllHostAddr


推荐