Note that there are some explanatory texts on larger screens.

plurals
  1. POPinging computer by name in Java
    primarykey
    data
    text
    <p>I am in the process of writing a simple program that extracts computer names from MySQL Database then stores those names into a String array list (this part works fine). After that I wrote a class and a method that takes a String as a parameter (which will be the computer name) and tries to ping it. Here is the code for that class:</p> <pre><code>public class Ping { public void pingHost(String hostName) { try { InetAddress inet = InetAddress.getByName(hostName); boolean status = inet.isReachable(5000); if (status) { System.out.println(inet.getHostName() + " Host Reached\t" + inet.getHostAddress()); } else { System.out.println(inet.getHostName() + " Host Unreachable"); } } catch (UnknownHostException e) { System.err.println(e.getMessage() + " Can't Reach Host"); } catch (IOException e) { System.err.println(e.getMessage() + " Error in reaching the Host"); } } </code></pre> <p>The problem is that I keep getting <code>UnknownHostException</code> thrown for most computers even if I can ping them manually or if I hard code the computer name as the "hostName". </p> <p>Here is what my main looks like:</p> <pre><code>public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException { ArrayList &lt;String&gt; list = new ArrayList&lt;String&gt;(); MySQLConnect myConnection = new MySQLConnect(); myConnection.importData(list); Ping pingComputer = new Ping(); pingComputer.pingHost(list.get(87)); } </code></pre> <p>Right now I'm just trying to experiment with one computer which is throwing <code>UnknownHostException</code> but can be pinged manually. Anyone have any idea why this is happening?</p> <p>EDIT...</p> <p>Just to explain this a little bit more. For example in main, if I pass these parameters to <code>pingHost</code>:</p> <pre><code>pingComputer.pingHost("ROOM-1234"); </code></pre> <p>It pings fine and returns correct host name/address. But <code>list.get(87)</code> returns same host name "ROOM-1234" but throws <code>UnknownHostException</code>? This has got me really confused and not sure why its not working.</p> <p>EDIT</p> <p>Wow finally figured it out. Reason ping was working when I was passing the string directly like so "ROOM-1234", was because there were no white spaces and getting is from array like so <code>list.get(87)</code> returned same thing but when I checked <code>charLength</code>, it returned a different value :) So I just ended up using <code>trim</code> to get rid of white spaces and now itworks great.</p> <pre><code>pingComputer.pingHost(list.get(87).trim()); </code></pre> <p>Thanks for all the suggestions!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload