
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java program to display Hostname and IP address
To display Hostname and IP address in Java, the code is as follows −
Example
import java.net.*; public class Demo{ public static void main(String[] args){ try{ InetAddress my_address = InetAddress.getLocalHost(); System.out.println("The IP address is : " + my_address.getHostAddress()); System.out.println("The host name is : " + my_address.getHostName()); } catch (UnknownHostException e){ System.out.println( "Couldn't find the local address."); } } }
Output
The IP address is : 127.0.0.1 The host name is : jdoodle
A class named Demo contains the main function. In this main function, a ‘try’ and ‘catch’ block is defined. In the ‘try’ block, an instance of InetAddress is created and the ‘getLocalHost’ function is used to get the Host address and host name of the InetAddress instance. In case one of the attributes is not found, the ‘catch’ block defines catching the exception and printing the relevant message on the console.
- Related Articles
- Program to display hostname and IP address C
- Python program to Display Hostname and IP address?
- C# program to Display Hostname and IP address
- C Program to display hostname and IP address
- Java program to convert Byte array to IP Address
- Java program to Get IP address of the system
- Java program to find IP Address of the client
- Difference between Static IP Address and Dynamic IP Address
- C Program to validate an IP address
- JavaScript program to retrieve clients IP address
- Display the Host Name and IP Address on a Tkinter Window
- Difference between IP Address and MAC Address
- Difference between MAC Address and IP Address
- How to display the IP Address of the Machine using C#?
- Program to convert IP address to hexadecimal in C++

Advertisements