3 Replies Latest reply on Mar 14, 2013 2:46 AM by 996701

    InetAddress Error

      I'm a newbie , and I've short program:

      import java.net.*;

      public class timdcIP {
           public static void main(String[] args){
                          System.out.println("Searching IP Addresss: ");
                     InetAddress host= new InetAddress.setByName(args[0]);
                     String HostName=host.getHostName();
                     System.out.println("HostName : "+ HostName);
                     System.out.println("IP Address : "+ host.getHostAddress());
                }catch(UnknownHostException e){
                     System.out.println("Can't search IP Address");


      And the Error:
      Exception in thread "main" java.lang.Error: Unresolved compilation problem:
           InetAddress.setByName cannot be resolved to a type

           at timdcIP.main(timdcIP.java:10)

      Please help me to fix this problem. Thank you very much!
        • 1. Re: InetAddress Error
          Hi first you need to understand how to construct InetAddress object. There is no setByName() method in InetAddress class(Refert to API : http://docs.oracle.com/javase/6/docs/api/java/net/InetAddress.html) and there is only getByName().
          You shouldn't use new keyword to create InetAddress object by calling static method getByName() of InetAddress class.

          Here is the modified source code to get the work done
          import java.net.InetAddress;
          import java.net.UnknownHostException;
          public class timdcIP {
               public static void main(String[] args) {
                    try {
                         if (args.length != 1) {
                              System.out.println("Searching IP Addresss: ");
                         InetAddress host = InetAddress.getByName("forums.oracle.com"); // No setByName() and no new keyword
                         String HostName = host.getHostName();
                         System.out.println("HostName : " + HostName);
                         System.out.println("IP Address : " + host.getHostAddress());
                    } catch (UnknownHostException e) {
                         System.out.println("Can't search IP Address");
          And final thing, post your source code in  segment from the next time onwards.

          1 person found this helpful
          • 2. Re: InetAddress Error
            Your main problem is that you have invented methods that don't exist, instead of reading the Javadoc. Your second problem was that you tried to execute a program that didn't compile.

            Use the Javadoc. Thats what it's for. Don't just make it up as you go along.
            • 3. Re: InetAddress Error
              thanh you!