Search code examples
javaintellij-ideainetaddress

Can not find symbol InetAddress.getByAddress()


I'm trying to make a connection to a web server and check if it's reachable from my computer. The problem is that when I try to use InetAddress.getByName(address) I get a compilation error:

Error:(20, 50) java: cannot find symbol
symbol:   class getByName
location: class java.net.InetAddress

It's weird because when I start to type this method name, IntelliJ IDEA shows me a tip with name suggestion list, so I guess there's no problem with wrong PATH variable. And when I'm done with typing method name it immediatelly paints in red and say "Can not resolve symbol by name". Meanwhile, InetAddress.isReachible(100) doesn't have such a problem, even considering that it's placed at the same class. Actually, I get the problem only with public static methods, such as InetAddress.getByAddress(), InetAddress.getLoopbackAddress() etc. I thought that I might use old java version, so I typed java -version in the windows command line, and here what it says:

java version "1.8.0_73"
Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)

And this is my code:

String url = "http://reddit.com";
    byte [] address = url.getBytes();
    InetAddress inetAddress = new InetAddress.getByName(address);
    try {
        inetAddress.isReachable(100);
    } catch (IOException e) {
        e.printStackTrace();
    }

Solution

  • Remove the new. Your code should be:

    InetAddress inetAddress = InetAddress.getByName(address);