Posts: 2
Joined: Mon Jul 08, 2013 9:36 am

java getting real ip addres

Mon Jul 08, 2013 9:53 am

Hi all,

in my code i?m using the following to get the ip address of the pi:

this is only returning and not the real ip adress which I need to ping the pi from another computer.
So how do i get the real ip adress from the raspberry with java?

Thx a lot.

User avatar
Posts: 1382
Joined: Sat Sep 15, 2012 10:35 am
Contact: Website

Re: java getting real ip addres

Mon Jul 08, 2013 11:27 am

Just a guess but I think it is just reading the info from the host file something like

Code: Select all


Code: Select all


This was helpful to me

Code: Select all


 * Simple Java program to find IP Address of localhost. This program uses
 * InetAddress from package to find IP address.
 * @author Javin Paul
public class IPTest {
    public static void main(String args[]) throws UnknownHostException {
        InetAddress addr = InetAddress.getLocalHost();
        //Getting IPAddress of localhost - getHostAddress return IP Address
        // in textual format
        String ipAddress = addr.getHostAddress();
        System.out.println("IP address of localhost from Java Program: " + ipAddress);
        String hostname = addr.getHostName();
        System.out.println("Name of hostname : " + hostname);

IP address of localhost from Java Program:
Name of hostname : PCLOND3433

Read more: ... ss-of.html - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)

Posts: 1
Joined: Mon Jan 01, 2018 1:53 pm

Re: java getting real ip addres

Mon Jan 01, 2018 1:57 pm

Well, for the question you asked here's the code,

Code: Select all


public class DisplayHostName
   public static void main(String[] args)
         InetAddress inet = InetAddress.getLocalHost();
         // getHostAddress() - this method returns the IP address
         System.out.println("Machine IP address : " + inet.getHostAddress());
         // getHostName() - this method returns the host name
         System.out.println("Host name : " + inet.getHostName()); 
      catch(UnknownHostException uhe)
         System.out.println("Sorry!! Could not find local address and hostname!!"); 
For more on getting IP address in java refer this resource - ... a-program/

User avatar
Posts: 33848
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: java getting real ip addres

Mon Jan 01, 2018 3:35 pm

That code doesn't work. It returns (which is defined in /etc/hosts).

I found this example

Code: Select all

import java.util.*;
import static java.lang.System.out;

public class ListNets {

    public static void main(String args[]) throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        for (NetworkInterface netint : Collections.list(nets))

    static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
        out.printf("Display name: %s\n", netint.getDisplayName());
        out.printf("Name: %s\n", netint.getName());
        Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
        for (InetAddress inetAddress : Collections.list(inetAddresses)) {
            out.printf("InetAddress: %s\n", inetAddress);
which works better and gives both the IPv4 and IPv6 addresses.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Posts: 223
Joined: Thu Oct 25, 2012 7:34 am

Re: java getting real ip addres

Mon Jan 08, 2018 3:53 pm

Example given by @DougieLawson is the right way of fetching (all) IP addresses of the RPi.
It can be improved a bit by filtering out loopbak and link local addresses:

Code: Select all

if (!address.isLoopbackAddress() && !address.isLinkLocalAddress()) {
Whenever you need to 'return' your IP address - consider returning a list of IP addresses (including empty list and list with more than one element) as your RPi can be connected to network using ethernet and wifi at the same time, etc... If you really insist on one and only IP from eth0 interface - then using name 'eth0' when filtering (netint.getName().equals('eth0'))...

Return to “Java”

Who is online

Users browsing this forum: No registered users and 4 guests