相关疑难解决方法(0)

如何在Java中确定Internet网络接口

如何确定使用Java连接到Internet的网络接口?例如,我跑

InetAddress.getLocalHost().getHostAddress();
Run Code Online (Sandbox Code Playgroud)

在Eclipse中,这完全返回我想要的内容,192.168.1.105.但是,如果我将其打包到jar文件并运行该程序,则代码返回169.254.234.50.看看这个,我发现这是我机器上的VMware虚拟以太网适配器接口的IP地址.

有没有办法确定连接到互联网的接口,但同时保持我的代码的可移植性?

接口比较

界面[net4]

display name  : Intel(R) Centrino(R) Ultimate-N 6300 AGN
MTU           : 1500
loopback      : false
point to point: false
up            : true
virtual       : false
multicast     : true
HW address    : 00 24 D7 2C 5F 70 
INET address (IPv4): 192.168.1.105
  host name           : MyComputer
  canonical host name : MyComputer
  loopback            : false
  site local          : true
  any local           : false
  link local          : false
  multicast           : false
  reachable           : true
Run Code Online (Sandbox Code Playgroud)

界面[eth5]

display name  : …
Run Code Online (Sandbox Code Playgroud)

java networking network-interface

13
推荐指数
2
解决办法
3万
查看次数

用Java获取系统的MAC地址

我需要获取运行该程序的系统的mac地址.但我无法做到这一点.

我正在编写以下代码:

public class App{

       public static void main(String[] args){

        InetAddress ip;
        try {

            ip = InetAddress.getLocalHost();
            System.out.println("Current IP address : " + ip.getHostAddress());

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);

            byte[] mac = network.getHardwareAddress();

            System.out.print("Current MAC address : ");

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));        
            }
            System.out.println(sb.toString());

        } catch (UnknownHostException e) {

            e.printStackTrace();

        } catch (SocketException e){

            e.printStackTrace();

        }

       }

    }
Run Code Online (Sandbox Code Playgroud)

我得到以下结果: …

java

9
推荐指数
1
解决办法
3万
查看次数

使用getHardwareAddress非确定性获取Java中的mac地址

我在获取机器的mac地址时遇到了问题,使用以下代码解决了这个问题:

Process p = Runtime.getRuntime().exec("getmac /fo csv /nh"); 
java.io.BufferedReader in = new java.io.BufferedReader(new  java.io.InputStreamReader(p.getInputStream())); 
String line; 
line = in.readLine();         
String[] result = line.split(","); 

System.out.println(result[0].replace('"', ' ').trim()); 
Run Code Online (Sandbox Code Playgroud)

但是,我想知道为什么这段代码不起作用.每次读取MAC地址时,它都会返回不同的值.首先我认为这是因为getHash,可能使用我不知道的时间戳......但即使删除它,结果也会改变.

    public static byte[] getMacAddress() {
        try {
            Enumeration<NetworkInterface> nwInterface = NetworkInterface.getNetworkInterfaces();
            while (nwInterface.hasMoreElements()) {
                NetworkInterface nis = nwInterface.nextElement();
                if (nis != null) {
                    byte[] mac = nis.getHardwareAddress();
                    if (mac != null) {
                        /*
                         * Extract each array of mac address and generate a
                         * hashCode for it
                         */
                        return mac;//.hashCode(); …
Run Code Online (Sandbox Code Playgroud)

java mac-address

7
推荐指数
2
解决办法
2万
查看次数

如何获得机器的mac地址

我想得到机器的MAC地址..但是下面写的代码只显示互联网连接到我的机器的MAC地址,否则它将返回null ...我正在使用Windows 7

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

class test
{
    public static void main(String[] args)
    {
        InetAddress ip;
        try {
            ip = InetAddress.getLocalHost();

            System.out.println("The mac Address of this machine is :" + ip.getHostAddress());

            NetworkInterface network = NetworkInterface.getByInetAddress(ip);

            byte[] mac = network.getHardwareAddress();

            System.out.print("The mac address is : ");

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++){
                sb.append(String.format("%02X%s", mac[i],(i< mac.length - 1)?"-":""));
            }

            System.out.println(sb.toString());

        } 
        catch (UnknownHostException e) {
            e.printStackTrace();
        } 
        catch …
Run Code Online (Sandbox Code Playgroud)

java

3
推荐指数
2
解决办法
1万
查看次数

如何在java或scala中获取localhost网络接口

我正在尝试在scala应用程序中获取我的机器的MAC地址.搜索时有几个结果,但它们都使用如下所示的内容,InetAddress.getLocalHost()后面跟着NetworkInterface.getByInetAddress(...):使用Java在本地计算机上获取MAC地址

我的问题是结果最终为null:

val localhost = InetAddress.getLocalHost
println(s"lh: $localhost")
val localNetworkInterface = NetworkInterface.getByInetAddress(localhost)
println(s"lni: $localNetworkInterface")

>>lh: ubuntu/127.0.1.1
>>lni: null
Run Code Online (Sandbox Code Playgroud)

java scala

3
推荐指数
1
解决办法
1613
查看次数

标签 统计

java ×5

mac-address ×1

network-interface ×1

networking ×1

scala ×1