如何在Java中运行具有策略权限的小程序来读取MAC地址 [英] How to run applet with policy permissions in Java to read the MAC address

查看:37
本文介绍了如何在Java中运行具有策略权限的小程序来读取MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 NetBeans 中开发一个可以读取 MAC 地址的小程序.

这是我的目录结构

这是我的更新代码

<块引用>

MacAddrApplet.java

import java.awt.Graphics;导入 java.net.NetworkInterface;导入 java.net.SocketException;导入 java.security.AccessController;导入 java.security.PrivilegedAction;导入 java.util.ArrayList;导入 java.util.Enumeration;导入 javax.swing.JApplet;/**** @author jay.patel*/公共类 MacAddressApplet 扩展 JApplet {公共静态字符串 getMacFromInterface(NetworkInterface ni) 抛出 SocketException {字节 mac[] = ni.getHardwareAddress();如果(mac != null){StringBuilder macAddress = new StringBuilder("");字符串 sep = "";for (byte o : mac) {macAddress.append(sep).append(String.format("%02X", o));sep = ":";}返回 macAddress.toString();}返回 "";}公共静态字符串[] getInterfaces() {尝试 {枚举<网络接口>nis = NetworkInterface.getNetworkInterfaces();ArrayList结果 = 新的 ArrayList();而(nis.hasMoreElements()){网络接口 ni = nis.nextElement();if (ni.isUp() && !ni.isLoopback() && !ni.isVirtual()) {String mac = getMacFromInterface(ni);String str = ni.getDisplayName() + ";"+ 麦克;结果.add(str);}}return result.toArray(new String[0]);} catch (SocketException e) {System.out.println("SocketException::" + e.getMessage());} 捕获(异常 e){System.out.println("异常::" + e.getMessage());}返回新字符串[0];}公共静态字符串 getInterfacesJSON() {尝试 {String macs[] = getInterfaces();字符串 sep = "";StringBuilder macArray = new StringBuilder("['");for (String mac : macs) {macArray.append(sep).append(mac);sep = "','";}macArray.append("']");返回 macArray.toString();} 捕获(异常 e){System.out.println("异常::" + e.getMessage());}返回 "[]";}@覆盖公共无效油漆(图形g){super.paint(g);AccessController.doPrivileged(new PrivilegedAction() {公共对象运行(){String macs[] = getInterfaces();for (String mac : macs) {System.out.println(" 接口 = " + mac);}System.out.println("接口JSON = " + getInterfacesJSON());g.drawString("MAC:" + getInterfacesJSON(), 100, 100);返回空;}});}}

<块引用>

MacAddrApplet.html

<头部><TITLE>小程序 HTML 页面</TITLE><身体><H3><HR WIDTH="100%">Applet HTML 页面<HR WIDTH="100%"></H3><P><APPLET codebase="classes" code="MacAddrApplet.class" width=350 height=200></APPLET></P><HR WIDTH="100%"><FONT SIZE=-1><I>由 NetBeans IDE 生成</I></FONT></身体>

<块引用>

applet.policy

grant {权限 java.security.AllPermission;};

<小时>

当我尝试从 Run -> 运行我的小程序时运行文件它工作得很好,并在其中显示了我的Mac地址,以及我正在打印的这个日志

Interfaces = en1;XX:XX:XX:XX:XX:XX接口 JSON = ['en1;XX:XX:XX:XX:XX:XX']

但是当我尝试使用

运行它时

appletviewer build/MacAddrApplet.html

打印

Interfaces = en1;接口 JSON = ['en1;']

我该如何解决这个问题?

附言我认为这是因为它没有使用 applet.policy

的权限

解决方案

最终我发现终端命令有什么问题

appletviewer build/MacAddrApplet.html

因为如果您看到位于 nbproject 文件夹中的 project.properties 文件,在最后部分,您会看到

run.jvmargs=-Djava.security.policy=applet.policy

所以当我使用 Run -> 运行小程序时运行文件 Netbeans 在内部处理了所有事情,因此小程序运行得非常好.

但是,在终端的情况下,我需要明确指定policy file(这是我提问时想知道的!),所以使用此命令运行小程序,它会正常工作...

appletviewer -J-Djava.security.policy=applet.policy build/MacAddrApplet.html

I'm trying to develop an applet in NetBeans which can read the MAC address.

So here is my directory structure

Here is my UPDATED Code

MacAddrApplet.java

import java.awt.Graphics;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Enumeration;
import javax.swing.JApplet;

/**
 *
 * @author jay.patel
 */
public class MacAddressApplet extends JApplet {

    public static String getMacFromInterface(NetworkInterface ni) throws SocketException {
        byte mac[] = ni.getHardwareAddress();

        if (mac != null) {
            StringBuilder macAddress = new StringBuilder("");
            String sep = "";
            for (byte o : mac) {
                macAddress.append(sep).append(String.format("%02X", o));
                sep = ":";
            }
            return macAddress.toString();
        }

        return "";
    }

    public static String[] getInterfaces() {
        try {
            Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();

            ArrayList<String> result = new ArrayList<String>();
            while (nis.hasMoreElements()) {
                NetworkInterface ni = nis.nextElement();
                if (ni.isUp() && !ni.isLoopback() && !ni.isVirtual()) {
                    String mac = getMacFromInterface(ni);
                    String str = ni.getDisplayName() + ";" + mac;
                    result.add(str);
                }
            }
            return result.toArray(new String[0]);
        } catch (SocketException e) {
            System.out.println("SocketException:: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("Exception:: " + e.getMessage());
        }

        return new String[0];
    }

    public static String getInterfacesJSON() {
        try {
            String macs[] = getInterfaces();

            String sep = "";
            StringBuilder macArray = new StringBuilder("['");
            for (String mac : macs) {
                macArray.append(sep).append(mac);
                sep = "','";
            }
            macArray.append("']");

            return macArray.toString();
        } catch (Exception e) {
            System.out.println("Exception:: " + e.getMessage());
        }

        return "[]";
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
         AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                String macs[] = getInterfaces();

                for (String mac : macs) {
                    System.out.println(" Interfaces = " + mac);
                }

                System.out.println(" Interfaces JSON = " + getInterfacesJSON());
                g.drawString("MAC: " + getInterfacesJSON(), 100, 100);
                return null;
            }
        });

    }

}

MacAddrApplet.html

<HTML>
<HEAD>
    <TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>

    <H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>

    <P><APPLET codebase="classes" code="MacAddrApplet.class" width=350 height=200></APPLET></P>

    <HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>

applet.policy

grant {
permission java.security.AllPermission;
};


When I try to run my applet from Run -> Run File it works perfectly fine and shows my Mac address in it, with this log that I'm printing

Interfaces = en1;XX:XX:XX:XX:XX:XX
Interfaces JSON = ['en1;XX:XX:XX:XX:XX:XX']

But when I try to run it using

appletviewer build/MacAddrApplet.html

it prints

Interfaces = en1;
Interfaces JSON = ['en1;']

How can I solve this issue??

P.S. I think it's happening because it's not using the permissions of applet.policy

解决方案

Eventually I figured out what's wrong with the terminal command

appletviewer build/MacAddrApplet.html

because if you see the project.properties file located in nbproject folder, at the end part, you'll see

run.jvmargs=-Djava.security.policy=applet.policy

So when I run the applet using Run -> Run File Netbeans internally took care of everything and hence the applet is running perfectly fine.

But, in case of the terminal, I need to specify the policy file explicitly (This is what I want to know when I asked the question!), So run the applet with this command and it will work fine...

appletviewer -J-Djava.security.policy=applet.policy build/MacAddrApplet.html

这篇关于如何在Java中运行具有策略权限的小程序来读取MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆