Results 1 to 14 of 14
  1. #1
    kracer is offline Member
    Join Date
    May 2010
    Location
    France, Paris
    Posts
    9
    Rep Power
    0

    Default What's wrong with my (simple) code

    Java Code:
    import java.net.InetAddress;
    public class Test2 {
    	public static void main (String[] args) {
    		InetAddress localaddr = InetAddress.getLocalHost();
    		System.out.println ("Local IP Address : " + localaddr);
    		System.out.println ("Local hostname   : " + localaddr.getHostName());
    	}
    }
    I have this message when it si compiled
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    
    	at first.Test2.main(Test.java:13)

  2. #2
    kracer is offline Member
    Join Date
    May 2010
    Location
    France, Paris
    Posts
    9
    Rep Power
    0

    Default

    The line 13 is actually the line"public static void main (String[] args) {".
    I didn't paste the whole code.

    Thanks

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't see you handling an exception:
    Java Code:
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class Test2 {
      public static void main(String[] args) throws UnknownHostException {
        InetAddress localaddr = InetAddress.getLocalHost();
        System.out.println("Local IP Address : " + localaddr);
        System.out.println("Local hostname   : " + localaddr.getHostName());
      }
    }
    What happens if you do this?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by kracer View Post
    I didn't paste the whole code.
    Ah, since you don't know the cause of the error, it may be in your best interest to do a little debugging to isolate it. Try to find the smallest bit of compilable code that demonstrates this error and post it.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    What surprises me is that the compiler generated a class file despite the fact that a (checked) UnknownHostException wasn't trapped or declared to be thrown. I bet it at least warned about that fact (and should've aborted the entire compilation task without generating a .class file).

    kind regards,

    Jos

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JosAH View Post
    What surprises me is that the compiler generated a class file despite the fact that a (checked) UnknownHostException wasn't trapped or declared to be thrown. I bet it at least warned about that fact (and should've aborted the entire compilation task without generating a .class file).
    Who knows what happened with this code, since the snippet provided was not complete and self-contained. Perhaps the exception was handled in the real code, perhaps not. Hence my request for small compilable code.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    Who knows what happened with this code, since the snippet provided was not complete and self-contained. Perhaps the exception was handled in the real code, perhaps not. Hence my request for small compilable code.
    True, but still, one way or the other some .class file was generated and the jvm had to catch the exception (a compilation problem, caught during runtime). The OP sure did some strange things (unknowingly) ...

    kind regards,

    Jos

  8. #8
    kracer is offline Member
    Join Date
    May 2010
    Location
    France, Paris
    Posts
    9
    Rep Power
    0

    Default

    Here is the whole code.
    [CODE]
    package myTestingPackage;

    import java.net.InetAddress;

    public class Test2 {
    public static void main (String[] args) {
    InetAddress localaddr = InetAddress.getLocalHost();
    System.out.println ("Local IP Address : " + localaddr);
    System.out.println ("Local hostname : " + localaddr.getHostName());
    }
    }
    [\CODE]
    And here uis the error message. It has changed but I don't know why!
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Unhandled exception type UnknownHostException
    
    	at myTestingPackage.Test2.main(Test2.java:7)

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kracer View Post
    And here uis the error message. It has changed but I don't know why!
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Unhandled exception type UnknownHostException
    
    	at myTestingPackage.Test2.main(Test2.java:7)
    Read the API documentation for the getLocalHost() method: it can throw the checked UnknownHostException. You either have to catch it or decare your main( ... ) method to throw it as well.

    kind regards,

    Jos

  10. #10
    kracer is offline Member
    Join Date
    May 2010
    Location
    France, Paris
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I don't see you handling an exception:
    Java Code:
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class Test2 {
      public static void main(String[] args) throws UnknownHostException {
        InetAddress localaddr = InetAddress.getLocalHost();
        System.out.println("Local IP Address : " + localaddr);
        System.out.println("Local hostname   : " + localaddr.getHostName());
      }
    }
    What happens if you do this?
    It works with your idea.
    But I don't know why it does not work without the "throws UnknownHostException".
    This may be a simple question. I am new to JAVA.

    Thank you.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kracer View Post
    It works with your idea.
    But I don't know why it does not work without the "throws UnknownHostException".
    This may be a simple question. I am new to JAVA.

    Thank you.
    Checked exceptions have to be handled by your code; either by declaring that your code lets it pass "throws ACheckedException" or by actually catching the exception and handling it. The compiler checks this and refuses to compile any code that doesn't handle such exceptions. It's a safety net.

    kind regards,

    Jos

  12. #12
    kracer is offline Member
    Join Date
    May 2010
    Location
    France, Paris
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Read the API documentation for the getLocalHost() method: it can throw the checked UnknownHostException. You either have to catch it or decare your main( ... ) method to throw it as well.

    kind regards,

    Jos
    Thank you Jos.
    So does this metohd throw always an UnkownHostException? Even if the IP address for the host could be found?

  13. #13
    kracer is offline Member
    Join Date
    May 2010
    Location
    France, Paris
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Checked exceptions have to be handled by your code; either by declaring that your code lets it pass "throws ACheckedException" or by actually catching the exception and handling it. The compiler checks this and refuses to compile any code that doesn't handle such exceptions. It's a safety net.

    kind regards,

    Jos
    OK, it actually answers my latest question.

    Thanks

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kracer View Post
    Thank you Jos.
    So does this metohd throw always an UnkownHostException? Even if the IP address for the host could be found?
    If a method states that it can throw an Exception it isn't obliged to always throw that Exception. It only throws it in exceptional circumstances. This method only throws that Exception if it can't find a local host and your code is supposed to handle that Exception when/if it is thrown because it is a 'checked' Exception. Your compiler checks it (as you already have noticed).

    kind regards,

    Jos

Similar Threads

  1. Need help! something wrong in my code
    By novak100 in forum New To Java
    Replies: 2
    Last Post: 11-18-2009, 11:59 PM
  2. Replies: 4
    Last Post: 07-23-2009, 09:45 PM
  3. pls tell wat wrong with my code???
    By low224 in forum New To Java
    Replies: 13
    Last Post: 01-11-2009, 07:40 AM
  4. Simple Addition Program Outputting Wrong Value
    By carlodelmundo in forum New To Java
    Replies: 4
    Last Post: 08-05-2008, 03:37 AM
  5. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 08:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •