如何在Java中使用策略权限运行小程序以读取MAC地址 [英] How to run applet with policy permissions in Java to read the MAC address
问题描述
我正在尝试在NetBeans中开发一个可以读取MAC地址的小程序.
I'm trying to develop an applet in NetBeans which can read the MAC address.
这是我的目录结构
这是我的更新代码
MacAddrApplet.java
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
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
applet.policy
grant {
permission java.security.AllPermission;
};
当我尝试从Run -> Run File
运行我的applet时,它运行正常,并在其中显示了我的Mac地址,并显示了我正在打印的日志
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']
但是当我尝试使用
appletviewer build/MacAddrApplet.html
它打印
Interfaces = en1;
Interfaces JSON = ['en1;']
我该如何解决这个问题?
P.S.我认为是因为它没有使用applet.policy
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
因为您看到位于nbproject
文件夹中的project.properties
文件,所以在结尾部分,您会看到
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
因此,当我使用Run -> Run File
运行applet时,Netbeans内部会处理所有事情,因此applet运行得很好.
So when I run the applet using Run -> Run File
Netbeans internally took care of everything and hence the applet is running perfectly fine.
但是,在终端的情况下,我需要显式指定policy file
(这是我问问题时要知道的内容!),因此使用此命令运行applet它将正常工作...
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屋!