java如何查询本机ip地址和mac地址
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java如何查询本机ip地址和mac地址相关的知识,希望对你有一定的参考价值。
Java中可以使用程序来获取本地ip地址和mac地址,使用InetAddress这个工具类,示例如下:
import java.net.*;public class NetInfo
public static void main(String[] args)
new NetInfo().say();
public void say()
try
InetAddress i = InetAddress.getLocalHost();
System.out.println(i); //计算机名称和IP
System.out.println(i.getHostName()); //名称
System.out.println(i.getHostAddress()); //只获得IP
catch(Exception e)e.printStackTrace();
也可以通过命令行窗口来查看本地ip和mac地址,输入命令:ipconfig。
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
public class getIpAndMac
public static void main(String[] args) throws UnknownHostException,
SocketException
InetAddress addr = InetAddress.getLocalHost();
String ip = addr.getHostAddress().toString();// 获得本机IP
System.out.println("本机ip:" + ip);
getLocalMac(addr);
private static void getLocalMac(InetAddress ia) throws SocketException
// TODO Auto-generated method stub
// 获取网卡,获取地址
byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mac.length; i++)
if (i != 0)
sb.append("-");
// 字节转换为整数
int temp = mac[i] & 0xff;
String str = Integer.toHexString(temp);
if (str.length() == 1)
sb.append("0" + str);
else
sb.append(str);
System.out.println("本机MAC地址:" + sb.toString().toUpperCase());
参考技术B import java.io.IOException;
import java.io.InputStreamReader;
public class GetMacAddress
public static String getOSName()
return System.getProperty("os.name").toLowerCase();
public static String getUnixMACAddress()
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try
process = Runtime.getRuntime().exec("ifconfig eth0");
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null)
index = line.toLowerCase().indexOf("hwaddr");
if (index != -1)
mac = line.substring(index +"hwaddr".length()+ 1).trim();
break;
catch (IOException e)
e.printStackTrace();
finally
try
if (bufferedReader != null)
bufferedReader.close();
catch (IOException e1)
e1.printStackTrace();
bufferedReader = null;
process = null;
return mac;
public static String getLinuxMACAddress()
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try
process = Runtime.getRuntime().exec("ifconfig eth0");
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null)
index = line.toLowerCase().indexOf("硬件地址");
if (index != -1)
mac = line.substring(index+4).trim();
break;
catch (IOException e)
e.printStackTrace();
finally
try
if (bufferedReader != null)
bufferedReader.close();
catch (IOException e1)
e1.printStackTrace();
bufferedReader = null;
process = null;
return mac;
public static String getWindowsMACAddress()
String mac = null;
BufferedReader bufferedReader = null;
Process process = null;
try
process = Runtime.getRuntime().exec("ipconfig /all");
bufferedReader = new BufferedReader(new InputStreamReader(process
.getInputStream()));
String line = null;
int index = -1;
while ((line = bufferedReader.readLine()) != null)
index = line.toLowerCase().indexOf("physical address");
if (index != -1)
index = line.indexOf(":");
if (index != -1)
mac = line.substring(index + 1).trim();
break;
catch (IOException e)
e.printStackTrace();
finally
try
if (bufferedReader != null)
bufferedReader.close();
catch (IOException e1)
e1.printStackTrace();
bufferedReader = null;
process = null;
return mac;
public static void main(String[] argc)
String os = getOSName();
System.out.println(os);
if(os.startsWith("windows"))
String mac = getWindowsMACAddress();
System.out.println("本地是windows:"+mac);
else if(os.startsWith("linux"))
String mac = getLinuxMACAddress();
System.out.println("本地是Linux系统,MAC地址是:"+mac);
else
String mac = getUnixMACAddress();
System.out.println("本地是Unix系统 MAC地址是:"+mac);
参考技术C IP地址:java.net.InetAddress类的 getLocalHost() 方法
MAC地址:没有直接的方法,百度搜“java 获得本机mac地址”有很多。
我的理解:因为java的网络程序是面向TCP/IP层的,MAC地址属于链路层了,在IP层的下面,java看不到。 所以要调用c和c++做的程序,比如Windows系统自带的ipconfig 参考技术D import java.net.*;
public class NetInfo
public static void main(String[] args)
new NetInfo().say();
public void say()
try
InetAddress i = InetAddress.getLocalHost();
System.out.println(i); //计算机名称和IP
System.out.println(i.getHostName()); //名称
System.out.println(i.getHostAddress()); //只获得IP
catch(Exception e)e.printStackTrace();
mac苹果电脑怎么查看本机IP与MAC地址
在windows系统中与Mac系统中查看本机ip地址有些不同,那么在Mac系统中该如何查看本机的IP地址以及物理MAC地址呢?给您带来相关的查看方法,具体步骤如下:
1、在Dock中点击“系统偏好设置”,进入到系统偏好设置界面。
2、在“互联网与无线”选项下,找到并点击“网络”这个设置。
3、在连接类型为“以太网”选项下,右面会有IP地址,掩码,以及路由器地址DNS地址等等。
4、如果你是宽带拨号连接,那么点击“宽带连接”选项,然后再点击右侧的“高级”按钮,这样便可以查看宽带拨号后的IP地址了!
4、打开终端,我们此处使用输入命令查询IP的方法实现IP地址的查询。
5、在终端中输入“ifconfig”然后回车,来查看IP地址
6、其中“eth0”表示以太网网卡,“lo0”表示环回地址。我的以太网网卡IP地址是“192.168.239.141”,MAC地址是“00:0c:29:26:bc:07”
以上是关于java如何查询本机ip地址和mac地址的主要内容,如果未能解决你的问题,请参考以下文章