我有以下代码,我的问题是当我从 netbeans 运行代码时它运行良好。但是当我从 jar 文件运行代码时,它不能正常工作。我希望 JAR 关闭它 MAC 不匹配,但它仍然保持运行 这是我的代码:
public static void main(String args[])throws Exception {
String[] macArray = new String[1000];
macArray[0] = "74 E5 43 23 F1 B4 ";
macArray[1] = "74 E5 43 24 5F 18 ";
int i=0;
String[] current = new String[1000];
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements())
{
NetworkInterface nif = interfaces.nextElement();
byte[] lBytes = nif.getHardwareAddress();
StringBuffer lStringBuffer = new StringBuffer();
if (lBytes != null)
{
for (byte b : lBytes)
{
lStringBuffer.append(String.format("%1$02X ", new Byte(b)));
}
}
current[i]=lStringBuffer.toString();
System.out.println(lStringBuffer);
i++;
//System.out.println(lStringBuffer)
}
for(; i<1000; i++){
current[i]= "" + 0;
}
int te=0;
for(int j=0; j<1000; j++){
for(int k=0; k<1000; k++){
if(current[j].equals(macArray[k])){
System.out.println("WOW!!!");
te=1;
}
}
//System.out.println(current[j]);
}
if(te!=1){
JOptionPane.showMessageDialog(null,"Error");
System.exit(0);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FormTTS().setVisible(true);
}
});
}
我认为我的代码中可能存在一些问题,所以我尝试了这个,但问题仍然存在:
public static void main(String args[])throws Exception {
int d, check=0;
String[] macArray;//array of allowed mac adresses
macArray = new String[1000];
macArray[0] = "74-E5-43-23-F1-B4";
//macArray[1] = "00-00-00-00-00-00";
//MAC CHECKING
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());
String a=(sb.toString());
//String b="74-E5-43-23-F1-B4";
for(d=0; d<macArray.length; d++){
if(a.equals(macArray[d])){
check=1;
break;
}
}
if(check==1){
System.out.println("Success: Congrats, you are registered");
}
if(check!=1){
System.out.println("Registration Required");
JOptionPane.showMessageDialog(null,"Error");
System.exit(0);
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e){
e.printStackTrace();
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new FormTTS().setVisible(true);
}
});
}