• 生成可执行jar


    pom.xml:

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>org.examplegroupId>
    7. <artifactId>ServerInfoPrintartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <build>
    10. <plugins>
    11. <plugin>
    12. <groupId>org.apache.maven.pluginsgroupId>
    13. <artifactId>maven-compiler-pluginartifactId>
    14. <configuration>
    15. <source>8source>
    16. <target>8target>
    17. configuration>
    18. plugin>
    19. plugins>
    20. build>
    21. project>

     Boot.java:

    1. import os.AbstractServerInfo;
    2. import os.LinuxServerInfo;
    3. import os.WindowsServerInfo;
    4. import java.util.List;
    5. public class Boot {
    6. public static void printServerInfo() throws Exception {
    7. String osName = System.getProperty("os.name").toLowerCase();
    8. AbstractServerInfo serverInfo;
    9. //根据不同操作系统类型选择不同的数据获取方法
    10. if (osName.startsWith("windows")) {
    11. serverInfo = new WindowsServerInfo();
    12. } else if (osName.startsWith("linux")) {
    13. serverInfo = new LinuxServerInfo();
    14. }else{//其他服务器类型
    15. serverInfo = new LinuxServerInfo();
    16. }
    17. System.out.println( "server info:" );
    18. System.out.println( "macAddress list:" );
    19. List macAddressList = serverInfo.getMacAddress();
    20. if( macAddressList == null ){
    21. return;
    22. }
    23. for( String macAddress:macAddressList ){
    24. System.out.println( " " + macAddress );
    25. }
    26. }
    27. public static void main(String[] args) throws Exception {
    28. printServerInfo();
    29. }
    30. }

    AbstractServerInfo.java:

    1. package os;
    2. import java.net.InetAddress;
    3. import java.net.NetworkInterface;
    4. import java.net.SocketException;
    5. import java.util.ArrayList;
    6. import java.util.Enumeration;
    7. import java.util.List;
    8. public abstract class AbstractServerInfo {
    9. /**
    10. * 获取Mac地址
    11. */
    12. public abstract List getMacAddress() throws Exception;
    13. /**
    14. * 获取当前服务器所有符合条件的InetAddress
    15. */
    16. protected List getLocalAllInetAddress() throws Exception {
    17. List result = new ArrayList<>(4);
    18. // 遍历所有的网络接口
    19. for (Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {
    20. NetworkInterface iface = (NetworkInterface) networkInterfaces.nextElement();
    21. // 在所有的接口下再遍历IP
    22. for (Enumeration inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
    23. InetAddress inetAddr = (InetAddress) inetAddresses.nextElement();
    24. //排除LoopbackAddress、SiteLocalAddress、LinkLocalAddress、MulticastAddress类型的IP地址
    25. if (!inetAddr.isLoopbackAddress() /*&& !inetAddr.isSiteLocalAddress()*/
    26. && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()) {
    27. result.add(inetAddr);
    28. }
    29. }
    30. }
    31. return result;
    32. }
    33. /**
    34. * 获取某个网络接口的Mac地址
    35. */
    36. protected String getMacByInetAddress(InetAddress inetAddr) {
    37. try {
    38. byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress();
    39. StringBuffer stringBuffer = new StringBuffer();
    40. for (int i = 0; i < mac.length; i++) {
    41. if (i != 0) {
    42. stringBuffer.append("-");
    43. }
    44. //将十六进制byte转化为字符串
    45. String temp = Integer.toHexString(mac[i] & 0xff);
    46. if (temp.length() == 1) {
    47. stringBuffer.append("0" + temp);
    48. } else {
    49. stringBuffer.append(temp);
    50. }
    51. }
    52. return stringBuffer.toString().toUpperCase();
    53. } catch (SocketException e) {
    54. e.printStackTrace();
    55. }
    56. return null;
    57. }
    58. }

     LinuxServerInfo.java:

    1. package os;
    2. import java.net.InetAddress;
    3. import java.util.List;
    4. import java.util.stream.Collectors;
    5. public class LinuxServerInfo extends AbstractServerInfo {
    6. @Override
    7. public List getMacAddress() throws Exception {
    8. List result = null;
    9. //1. 获取所有网络接口
    10. List inetAddresses = getLocalAllInetAddress();
    11. if (inetAddresses != null && inetAddresses.size() > 0) {
    12. //2. 获取所有网络接口的Mac地址
    13. result = inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList());
    14. }
    15. return result;
    16. }
    17. }

    WindowsServerInfo.java:

    1. package os;
    2. import java.net.InetAddress;
    3. import java.util.List;
    4. import java.util.stream.Collectors;
    5. public class WindowsServerInfo extends AbstractServerInfo {
    6. @Override
    7. public List getMacAddress() throws Exception {
    8. List result = null;
    9. //1. 获取所有网络接口
    10. List inetAddresses = getLocalAllInetAddress();
    11. if (inetAddresses != null && inetAddresses.size() > 0) {
    12. //2. 获取所有网络接口的Mac地址
    13. result = inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList());
    14. }
    15. return result;
    16. }
    17. }

    步骤:

            cmd
            cd E:\git\ServerInfoPrint
            mvn  clean
            mvn install
            cd E:\git\ServerInfoPrint\target\classes
            jar cvf ServerInfoPrinter.jar ./
            打开 ServerInfoPrinter.jar  中的 META-INF/MANIFEST.MF 文件,在最后面添加一行"Main-Class: Boot"
            java -jar ServerInfoPrinter.jar 

     

  • 相关阅读:
    内容分发网络 CDN
    Windows命令行查找并kill进程及常用批处理命令汇总
    全局异常处理器无法处理sql报错
    神经网络编译器TVM
    干货 | 携程火车票iOS项目开发体验优化实践
    《Vue.js 3.x+Element Plus前端开发实战》简介
    linux中如何配置静态ip模式?
    UI自动化测试-web浏览器-鼠标和键盘操作
    积加ERP与金蝶云星空对接集成日期范围报告查询打通销售出库新增
    VUE——验证码倒计时
  • 原文地址:https://blog.csdn.net/heshiyuan1406146854/article/details/126460869