Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to get your computer name and IP address?

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Post How to get your computer name and IP address?

    This code will print your workstations name and IP address:

    public class NetInfo {
         public static void main(String[] args) {
            new NetInfo().say();
            }
     
         public void say() {
           try {
           java.net.InetAddress i = java.net.InetAddress.getLocalHost();
           System.out.println(i);                  // name and IP address
           System.out.println(i.getHostName());    // name
           System.out.println(i.getHostAddress()); // IP address only
           }
           catch(Exception e){e.printStackTrace();}
         }
        }
    Sample output:

    homepc/10.210.65.8
    homepc
    10.210.65.8

    This code will list the interfaces available on a workstation:

    import java.net.*;
    import java.util.*;
    import java.io.*;
    import java.nio.*;
     
    public class IPAddress {
     public void  getInterfaces (){
          try {
             Enumeration e = NetworkInterface.getNetworkInterfaces();
     
             while(e.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) e.nextElement();
                System.out.println("Net interface: "+ni.getName());
     
                Enumeration e2 = ni.getInetAddresses();
     
                while (e2.hasMoreElements()){
                   InetAddress ip = (InetAddress) e2.nextElement();
                   System.out.println("IP address: "+ ip.toString());
                }
             }
          }
          catch (Exception e) {
             e.printStackTrace();
          }
       }
     
       public static void main(String[] args) {
        IPAddress ip = new IPAddress();
        ip.getInterfaces();
       }
    }
    Sample output:

    Net interface: lo
    IP address: /127.0.0.1
    Net interface: eth0
    IP address: /10.210.62.4
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  2. The Following 4 Users Say Thank You to JavaPF For This Useful Post:

    JanLW (January 30th, 2014), javapenguin (January 14th, 2011), Json (July 9th, 2009), sainathdi (January 18th, 2013)


  3. #2
    Junior Member
    Join Date
    Jun 2009
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Get your computers name and IP address

    Yea rite.Tanks

  4. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    17
    My Mood
    Confused
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: How to Get your computers name and IP address

    wow cool,, tnx 4 thiz!!

  5. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Get your computers name and IP address

    Awesome tutorial. This is very useful. I have one newbie question. I can get it to run inside of Netbeans no prob but it wont run as a standalone. Is there a command to make it so you can have it pop up via cmd in windows?

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to Get your computers name and IP address

    Use the default command for running a java program via command-line. Note that you will need to pass both class files to the jvm. Alternatively, you can set the classpath.

    java IPAddress CLASSPATH=%THE_PATH%
    rem the path is the path(s) of the class files. Only one path is required if they're both in the same folder

  7. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    Keiyentai (August 8th, 2010), Seiko09 (December 8th, 2011)

  8. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Get your computers name and IP address

    Thank you

  9. #7
    Junior Member
    Join Date
    Nov 2010
    Location
    Istanbul
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Get your computers name and IP address

    Thank you

  10. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to Get your computers name and IP address

    Thank you it was very usefull

Similar Threads

  1. Replies: 8
    Last Post: April 21st, 2013, 08:20 AM
  2. Replies: 3
    Last Post: December 22nd, 2011, 09:46 AM
  3. Java program to open and close computer CD/DVD drive
    By JavaPF in forum Java Programming Tutorials
    Replies: 0
    Last Post: January 28th, 2009, 12:04 PM

Tags for this Thread