3 Replies Latest reply: Mar 13, 2013 9:46 PM by 996701 RSS

    InetAddress Error

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


      import java.net.*;

      public class timdcIP {
           public static void main(String[] args){
                try{
                     if(args.length!=1){
                          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");
                     return;
                }
           }

      }
      ------------

      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
          UdayaShankaraGandhi
          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");
                         return;
                    }
               }
          
          }
          And final thing, post your source code in  segment from the next time onwards.

          Thanks,
          Uday
          • 2. Re: InetAddress Error
            EJP
            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
              996701
              thanh you!