1、打开eclipse软件,如图所示,当然也可以采用启动的java编程软件了。

2、建立一个带有main方法的java工程,如图所示;

3、编辑我们的main方法,其实仅仅就对代码挺少的;具体代码如下所示,由于InetAdress.getLocalHost()方法需要抛出一个异常所以就采用try catch结构了:
public class getiptext {
public static void main(String[] args) {
try{
InetAddress myip= InetAddress.getLocalHost();
System.out.println("你的IP地址是:"+myip.getHostAddress());
System.out.println("主机名为:"+myip.getHostName()+"。");
}catch(Exception e){
e.printStackTrace();
}
}
}

4、单击“编译与运行”按钮,我们可以查看的运行结果,看到自己的IP了;

5、为了验证IP的正确性,我们打开我们的电脑网络属性来查看自己的IP如图所示,看看是不是一样了啊。
