JDK-8366034 : URL equality fails for same URL string
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 8,25
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2025-08-20
  • Updated: 2025-08-23
  • Resolved: 2025-08-23
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE PROBLEM :
I believe that I've spotted the following in my servers:

```
import java.net.URL

String address  = // some http address
URL firstUrl = new URL(address)
URL secondUrl = new URL(address)

logger.info(firstUrl.equals(secondUrl).toString()) // prints false
```

I'm using the deprecated method but it doesn't really matter. My expectation is that in URLStreamHandler, hostEqual getHostAddress gets executed two times, and IP can change in between calls. It doesn't break reflection because the host is cached, but it is definitely surprising. In production with much more traffic this happens quite often.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Set networkaddress.cache.ttl=0 so that Java is never caching
2. Set a breakpoint in URLStreamHandler.java, in line "InetAddress a2 = getHostAddress(u2);"
3. Run the test code until the breakpoint
4. Change the /etc/hosts file overriding the URL from the test code, for example "1.1.1.1         www.google.com"
5. Resume the program and see the result

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Logger shows "true"
ACTUAL - 
Logger shows "false"

---------- BEGIN SOURCE ----------
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Logger;

public class Test{

    public static void main(String [] args) throws MalformedURLException{
        final Logger logger = Logger.getLogger(Test.class.getName());
        String address = "https://www.google.com/";
        
        URL firstUrl = new URL(address);
        URL secondUrl = new URL(address);

        logger.info(Boolean.toString(firstUrl.equals(secondUrl))); // prints false
    }
}
---------- END SOURCE ----------
Comments
As my colleague mentioned, this behavior cannot be changed due to existing specifications and implementation, where equals() resolves the hostnames to IP addresses rather than performing a simple string comparison. For this reason, developers are encouraged to use URI instead of URL.
23-08-2025

Observations on macOS 15.6 + IntelliJ: - JDK 8u461: Failed, result of equals is false - JDK 25+36: Failed, result of equals is false
23-08-2025

JDK-4434494 provides a summary of why this cannot be changed. Since JDK 1.4, the guidance to developers is to use URI rather than URL.
22-08-2025

Submitter's response ----------------------------------- In order to make it work, you need to make the domain change. The way I do it, is cat /etc/hosts.with_overrides ## contains 127.0.0.1 oracle.com cat /etc/hosts.no_overrides ## no override Then, I set a debugger in URLStreamHandler.java protected boolean hostsEqual(URL u1, URL u2) { InetAddress a1 = getHostAddress(u1); InetAddress a2 = getHostAddress(u2); After the first line. I wait a bunch to make sure the cache is evicted. I replace the /etc/hosts. So sudo cp /etc/hosts.with_overrides /etc/hosts Or the other way around. At that point the resolution is different and it fails. In production with much more traffic this happens quite often.
21-08-2025

Email to submitter ----------------------------------- I've tried to reproduce your issue, but I couldn't, I attached the code that I use to try reproducing it. Can you provide a reproducer of the issue? Thank you in advance!
20-08-2025