最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java代碼如何判斷l(xiāng)inux系統(tǒng)windows系統(tǒng)

 更新時(shí)間:2023年01月10日 08:38:16   作者:蒼穹之躍  
這篇文章主要介紹了Java代碼如何判斷l(xiāng)inux系統(tǒng)windows系統(tǒng)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java代碼判斷l(xiāng)inux系統(tǒng)windows系統(tǒng)

在使用硬件SDK的時(shí)候,往往有windows、linux兩套。

本地開發(fā)的時(shí)候使用windows,打包發(fā)布的時(shí)候使用linux,來回切換很麻煩。

可以使用下面的判斷來加載不同版本的SDK。

package Commom;
 
public class osSelect {
 
    public static boolean isLinux() {
        return System.getProperty("os.name").toLowerCase().contains("linux");
    }
 
    public static boolean isWindows() {
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }
 
}

Java在Linux與windows系統(tǒng)下獲取主板序列號(hào),cpu序列號(hào)以及mac地址

概述:

實(shí)現(xiàn)了獲取當(dāng)前操作系統(tǒng)名稱,主板序列號(hào),CPU序列號(hào),mac地址的相關(guān)方法函數(shù)。

應(yīng)對(duì)的場景是信創(chuàng)設(shè)備無法正常識(shí)別我們的加密狗,對(duì)于軟件license的限制,我們通過系統(tǒng)當(dāng)前日期以及綁定對(duì)方設(shè)備進(jìn)行限制。

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
 
public class DmcUtils {
 
 
	/**
	 * 獲取當(dāng)前操作系統(tǒng)名稱
	 */
	public static String getOSName() {
		return System.getProperty("os.name").toLowerCase();
	}
 
	// 主板序列號(hào) windows
	public static String getMainBordId_windows() {
		String result = "";
		try {
			File file = File.createTempFile("realhowto", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);
 
			String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
					+ "Set colItems = objWMIService.ExecQuery _ \n" + "   (\"Select * from Win32_BaseBoard\") \n"
					+ "For Each objItem in colItems \n" + "    Wscript.Echo objItem.SerialNumber \n"
					+ "    exit for  ' do the first cpu only! \n" + "Next \n";
 
			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				result += line;
			}
			input.close();
		} catch (Exception e) {
			System.out.print("獲取主板信息錯(cuò)誤");
			 
		}
		return result.trim();
	}
 
	// 主板序列號(hào) linux
	public static String getMainBordId_linux() {
 
		String result = "";
		String maniBord_cmd = "dmidecode | grep 'Serial Number' | awk '{print $3}' | tail -1";
		Process p;
		try {
			p = Runtime.getRuntime().exec(new String[] { "sh", "-c", maniBord_cmd });// 管道
			BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				result += line;
				break;
			}
			br.close();
		} catch (IOException e) {
			System.out.print("獲取主板信息錯(cuò)誤");
		}
		return result;
	}
 
	/**
	 * 獲取mac地址 (如果Linux下有eth0這個(gè)網(wǎng)卡)
	 */
	public static String getMAC_linux() {
		String mac = null;
		BufferedReader bufferedReader = null;
		Process process = null;
		try {
			// linux下的命令,一般取eth0作為本地主網(wǎng)卡
			process = Runtime.getRuntime().exec("ifconfig eth0");
			// 顯示信息中包含有mac地址信息
			bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line = null;
			int index = -1;
			while ((line = bufferedReader.readLine()) != null) {
				// 尋找標(biāo)示字符串[hwaddr]
				index = line.toLowerCase().indexOf("hwaddr");
				if (index >= 0) {// 找到了
					// 取出mac地址并去除2邊空格
					mac = line.substring(index + "hwaddr".length() + 1).trim();
					break;
				}
			}
		} catch (IOException e) {
			System.out.print("獲取mac信息錯(cuò)誤");
		 
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException e1) {
				System.out.print("獲取mac信息錯(cuò)誤");
			}
			bufferedReader = null;
			process = null;
		}
		return mac;
	}
 
        /*
         * 獲取Linux的mac
         */
        public static String getMAC_linuxs() {
		
		String mac = null;
		BufferedReader bufferedReader = null;
		Process process = null;
		try {
			// linux下的命令,一般取eth0作為本地主網(wǎng)卡
			process = Runtime.getRuntime().exec("ifconfig");
			// 顯示信息中包含有mac地址信息
			bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line = null;
			int index = -1;
			 while ((line = bufferedReader.readLine()) != null) 
			 {
				 Pattern pat = Pattern.compile("\\b\\w+:\\w+:\\w+:\\w+:\\w+:\\w+\\b");
				 Matcher mat= pat.matcher(line);
				 if(mat.find())
				 {
					 mac=mat.group(0);
				 }
			 }
 
		} catch (IOException e) {
			System.out.print("獲取mac信息錯(cuò)誤");
			 
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException e1) {
				System.out.print("獲取mac信息錯(cuò)誤");
				 
			}
			bufferedReader = null;
			process = null;
		}
		return mac;
	}
 
 
	/**
	 * 獲取widnows網(wǎng)卡的mac地址.
	 */
	public static String getMAC_windows() {
		InetAddress ip = null;
		NetworkInterface ni = null;
		List<String> macList = new ArrayList<String>();
		try {
			Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
					.getNetworkInterfaces();
			while (netInterfaces.hasMoreElements()) {
				ni = (NetworkInterface) netInterfaces.nextElement();
				// ----------特定情況,可以考慮用ni.getName判斷
				// 遍歷所有ip
				Enumeration<InetAddress> ips = ni.getInetAddresses();
				while (ips.hasMoreElements()) {
					ip = (InetAddress) ips.nextElement();
					if (!ip.isLoopbackAddress() // 非127.0.0.1
							&& ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
						macList.add(getMacFromBytes(ni.getHardwareAddress()));
					}
				}
			}
		} catch (Exception e) {
			System.out.print("獲取mac信息錯(cuò)誤");
			 
		}
		if (macList.size() > 0) {
			return macList.get(0);
		} else {
			return "";
		}
 
	}
 
	private static String getMacFromBytes(byte[] bytes) {
		StringBuffer mac = new StringBuffer();
		byte currentByte;
		boolean first = false;
		for (byte b : bytes) {
			if (first) {
				mac.append("-");
			}
			currentByte = (byte) ((b & 240) >> 4);
			mac.append(Integer.toHexString(currentByte));
			currentByte = (byte) (b & 15);
			mac.append(Integer.toHexString(currentByte));
			first = true;
		}
		return mac.toString().toUpperCase();
	}
 
	/**
	 * 獲取CPU序列號(hào) Windows
	 * 
	 * @return
	 */
	public static String getCPUID_Windows() {
		String result = "";
		try {
			File file = File.createTempFile("tmp", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);
			String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
					+ "Set colItems = objWMIService.ExecQuery _ \n" + "   (\"Select * from Win32_Processor\") \n"
					+ "For Each objItem in colItems \n" + "    Wscript.Echo objItem.ProcessorId \n"
					+ "    exit for  ' do the first cpu only! \n" + "Next \n";
 
			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				result += line;
			}
			input.close();
			file.delete();
		} catch (Exception e) {
			System.out.print("獲取mac信息錯(cuò)誤");
		}
		return result.trim();
	}
 
	/**
	 * 獲取CPU序列號(hào) linux
	 * 
	 * @return
	 */
	public static String getCPUID_linux() throws InterruptedException {
		String result = "";
		String CPU_ID_CMD = "dmidecode";
		BufferedReader bufferedReader = null;
		Process p = null;
		try {
			p = Runtime.getRuntime().exec(new String[] { "sh", "-c", CPU_ID_CMD });// 管道
			bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line = null;
			int index = -1;
			while ((line = bufferedReader.readLine()) != null) {
				// 尋找標(biāo)示字符串[hwaddr]
				index = line.toLowerCase().indexOf("uuid");
				if (index >= 0) {// 找到了
					// 取出mac地址并去除2邊空格
					result = line.substring(index + "uuid".length() + 1).trim();
					break;
				}
			}
 
		} catch (IOException e) {
			System.out.print("獲取mac信息錯(cuò)誤");
		}
		return result.trim();
	}
	
	public static void main(String [] args) throws Exception {
		System.out.println("開始獲?。?);
		String cpuId=getCPUID_linux();
		System.out.println("linux cpuId:"+cpuId);
		//String cpuId=  getCPUID_Windows();
		//System.out.println("Windows cpuId:"+cpuId);
		String bordId= getMainBordId_linux();
		System.out.println("linux bordId:"+bordId);
		//String bordId=  getMainBordId_windows();
		//System.out.println("Windows bordId:"+bordId);
		System.out.println("獲取結(jié)束!");
	}
 
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring mvc AJAX技術(shù)實(shí)現(xiàn)原理解析

    Spring mvc AJAX技術(shù)實(shí)現(xiàn)原理解析

    這篇文章主要介紹了Spring mvc AJAX技術(shù)實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • spring-boot-starter-validation?校驗(yàn)參數(shù)的實(shí)現(xiàn)

    spring-boot-starter-validation?校驗(yàn)參數(shù)的實(shí)現(xiàn)

    參數(shù)校驗(yàn)在很多地方都可以用到,本文主要介紹了spring-boot-starter-validation?校驗(yàn)參數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Java輕松掌握面向?qū)ο蟮娜筇匦苑庋b與繼承和多態(tài)

    Java輕松掌握面向?qū)ο蟮娜筇匦苑庋b與繼承和多態(tài)

    本文主要講述的是面向?qū)ο蟮娜筇匦裕悍庋b,繼承,多態(tài),內(nèi)容含括從封裝到繼承再到多態(tài)的所有重點(diǎn)內(nèi)容以及使用細(xì)節(jié)和注意事項(xiàng),內(nèi)容有點(diǎn)長,請(qǐng)大家耐心看完
    2022-05-05
  • Spring Data JPA使用JPQL與原生SQL進(jìn)行查詢的操作

    Spring Data JPA使用JPQL與原生SQL進(jìn)行查詢的操作

    這篇文章主要介紹了Spring Data JPA使用JPQL與原生SQL進(jìn)行查詢的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java編程關(guān)于子類重寫父類方法問題的理解

    Java編程關(guān)于子類重寫父類方法問題的理解

    這篇文章主要介紹了Java編程關(guān)于子類重寫父類方法問題的理解,分享了有關(guān)子類重寫父類的實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Java中的WeakHashMap簡析

    Java中的WeakHashMap簡析

    這篇文章主要介紹了Java中的WeakHashMap簡析,Map 的子類常見的有 HashMap、Hashtable、ConcurrentHashMap、LinkedHashMap 等,WeakHashMap,直譯就是,虛弱的 HashMap,從名字可得知其和 HashMap 有關(guān),需要的朋友可以參考下
    2023-09-09
  • Java解決刪除字符使頻率相同問題

    Java解決刪除字符使頻率相同問題

    給你一個(gè)下標(biāo)從0開始的字符串 word ,字符串只包含小寫英文字母,你需要選擇一個(gè)下標(biāo)并刪除下標(biāo)處的字符,使得word中剩余每個(gè)字母出現(xiàn)頻率相同,本文給大家介紹了Java解決刪除字符使頻率相同問題,需要的朋友可以參考下
    2024-02-02
  • SpringBoot發(fā)送各種復(fù)雜格式郵件的示例詳解

    SpringBoot發(fā)送各種復(fù)雜格式郵件的示例詳解

    本文主要介紹了如何使用JavaMailSender接口和MimeMessageHelper類,在SpringBoot實(shí)現(xiàn)發(fā)送帶有附件,嵌入資源,抄送和密送的復(fù)雜郵件,需要的可以了解下
    2024-11-11
  • 圖文詳解Java的反射機(jī)制

    圖文詳解Java的反射機(jī)制

    反射就是Reflection,Java的反射是指程序在運(yùn)行期可以拿到一個(gè)對(duì)象的所有信息。反射機(jī)制是框架的靈魂,一個(gè)java程序員不能不會(huì)使用反射,本文就來和大家一起詳細(xì)聊聊Java的反射機(jī)制
    2022-08-08
  • springboot攔截器不攔截靜態(tài)資源,只攔截controller的實(shí)現(xiàn)方法

    springboot攔截器不攔截靜態(tài)資源,只攔截controller的實(shí)現(xiàn)方法

    這篇文章主要介紹了springboot攔截器不攔截靜態(tài)資源,只攔截controller的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評(píng)論

中方县| 府谷县| 米泉市| 鸡泽县| 博乐市| 珠海市| 赤峰市| 宝鸡市| 金秀| 永登县| 宜春市| 左贡县| 太仓市| 江永县| 吉首市| 张北县| 汕头市| 陆良县| 江华| 平果县| 新竹市| 鄂州市| 迭部县| 深泽县| 建平县| 祁阳县| 沙雅县| 祁连县| 石城县| 济南市| 峨眉山市| 桃园县| 图木舒克市| 海宁市| 同江市| 平顶山市| 绥阳县| 宜宾县| 延安市| 宜章县| 荥阳市|