Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-15-2004, 01:03 PM   #1
LQ Newbie
Registered: Sep 2004
Posts: 1

Rep: Reputation: 0
Java code to get inet address in linux

hi all.. i encounter a problem. I have a servlet that is deploy in apache tomcat which is running the linux OS. I need to get the inet address in order to let client access the server.

I coded these lines of code in my servlet:

InetAddress localhost = InetAddress.getLocalHost();
String hostAddr = localhost.getHostAddress();

However, i encounter null for hostAddr but it works perfectly well in Windows. I want to get the ip address of the server, example: 203.204.423.52 and not

Anyone knows how to solve the above issue? Do i need to make any changes to the coding to cater for linux? Any suggestion or comment is welcome. Thanks!
Old 09-15-2004, 01:23 PM   #2
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
I got "" for that rather than null.
Anyway one way to overcome this is to use the NetworkInterface class instead to get the ip address for the interface that you want. The class is only in j2se 1.4 or greater though.

Is there a way to make the original code work??

Last edited by mirradric; 09-15-2004 at 01:24 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why won't this Java code work? ludeKing Programming 9 05-28-2004 11:32 AM
Java, code compiles and supposedly runs fine in windows, but not linux exodist Programming 4 03-16-2004 12:47 PM
how to invoke a linux command inside a java code ? kusum Linux - Software 2 11-23-2003 01:19 PM
Java application startup using inet.d lashworth Linux - Networking 0 08-28-2003 02:35 PM
Win95 slow inet access via Linux MASQ raypen Linux - Networking 0 11-28-2002 09:39 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:01 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration