• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 852
  • Last Modified:

Get local hostname in java

I would like to get the name of the host running my code.

I use the code below.
Unfortunately, this throws the exception, so something doesn't work.
Strangely enough however, the exception is:
java.net.UnknownHostException: Sylvanas: Sylvanas
        at java.net.InetAddress.getLocalHost(InetAddress.java:1353)
        at GetXML.main(GetXML.java:10)

Which contains the answer I'm looking for: "Sylvanas"
Why do I get an exception, and why does the exception contain the answer to my question, instead of "couldn't-find-it"
try {
  java.net.InetAddress localMachine = java.net.InetAddress.getLocalHost();
  System.out.println("Hostname of local machine: " + localMachine.getHostName());
} catch (java.net.UnknownHostException uhe) {
  System.out.println("Exception: " + uhe.getStackTrace());
}

Open in new window

0
letharion
Asked:
letharion
  • 4
  • 3
2 Solutions
 
objectsCommented:
because it can't find the ip address
0
 
objectsCommented:
it knows the name, but not the ip
0
 
letharionAuthor Commented:
Ok, maybe I have misunderstood what the function does.
Is there another way to get the hostname, so I can avoid the exception?
I could in theory parse the name from the exception, but that'd be really ugly.

Otherwise I'll just pass $(uname -n) as arg when calling the program, which relies on a *nix platform, but solves my problem.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
wily1Commented:
Sounds like there is something wrong (or incomplete) with DNS in your network, as "objects" says, the name is found but InetAddress isn't certain it's valid because it can't resolve the name to an IP address. (InetAddress is both name and IP).  

But if all you need is the hostname for logging or labelling purposes, you could design like so:
1) get InetAddress.getLocalHost().getHostName()
2a) catch UnknownHostException, parse hostname from stack
- or -
2b) look in shell env for hostname, Unix: "uname -n" or  $hostname,  Win: %HOSTNAME%
- or -
3) exit and requre hostname as command-line parameter
0
 
objectsCommented:
Suggest investigating why it cannot determine your ip
0
 
letharionAuthor Commented:
objects:
Is that important for any other reason? Do I have other potential problems waiting for me? Otherwise I'll just pass in the data from uname and be happy.
0
 
objectsCommented:
probably not, its a bit weird though.
0
 
letharionAuthor Commented:
Yeah, I was a bit surprised too
Thanks for the input
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now