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: Sylvanas: Sylvanas
        at GetXML.main(

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 { localMachine =;
  System.out.println("Hostname of local machine: " + localMachine.getHostName());
} catch ( uhe) {
  System.out.println("Exception: " + uhe.getStackTrace());

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
because it can't find the ip address
Mick BarryJava DeveloperCommented:
it knows the name, but not the ip

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

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
Mick BarryJava DeveloperCommented:
Suggest investigating why it cannot determine your ip
letharionAuthor Commented:
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.
Mick BarryJava DeveloperCommented:
probably not, its a bit weird though.
letharionAuthor Commented:
Yeah, I was a bit surprised too
Thanks for the input
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.