Last active
June 1, 2026 14:35
-
-
Save asdf913/13a7ad9d8a30617d5436813f3dc23f3f to your computer and use it in GitHub Desktop.
How to get the BIOS vendor under Linux,MacOSX and Microsoft Windows by Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.File; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.nio.charset.StandardCharsets; | |
| import java.nio.file.FileSystems; | |
| import java.nio.file.Files; | |
| import java.util.Collection; | |
| import java.util.Objects; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| import java.util.stream.Collectors; | |
| import org.apache.commons.io.IOUtils; | |
| import org.apache.commons.lang3.ObjectUtils; | |
| import org.apache.commons.lang3.StringUtils; | |
| import com.sun.jna.platform.win32.Ole32; | |
| import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery; | |
| import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult; | |
| public class BiosUtil { | |
| public static void main(final String[] args) throws IOException { | |
| // | |
| System.out.println(getVendor()); | |
| // | |
| } | |
| private static enum BiosProperty { | |
| Manufacturer | |
| } | |
| private static String getVendor() throws IOException { | |
| // | |
| final String name = getName(getClass(FileSystems.getDefault())); | |
| // | |
| if (Objects.equals(name, "sun.nio.fs.LinuxFileSystem")) { | |
| // | |
| final File file = new File("/sys/devices/virtual/dmi/id/bios_vendor"); | |
| // | |
| if (!file.exists() || !file.isFile() || !file.canRead()) { | |
| // | |
| return null; | |
| // | |
| } // if | |
| // | |
| return StringUtils.trim(Files.readAllLines(file.toPath()).stream().collect(Collectors.joining())); | |
| // | |
| } else if (Objects.equals(name, "sun.nio.fs.WindowsFileSystem")) { | |
| // | |
| final Ole32 ole32 = Ole32.INSTANCE; | |
| // | |
| if (ole32 != null) { | |
| // | |
| ole32.CoInitializeEx(null, Ole32.COINIT_APARTMENTTHREADED); | |
| // | |
| } // if | |
| // | |
| String manufacturer = null; | |
| // | |
| try { | |
| // | |
| final WmiResult<BiosProperty> result = new WmiQuery<>("Win32_ComputerSystem", BiosProperty.class) | |
| .execute(); | |
| // | |
| for (int i = 0; result != null && i < result.getResultCount(); i++) { | |
| // | |
| if (manufacturer != null) { | |
| // | |
| throw new IllegalStateException(); | |
| // | |
| } // if | |
| // | |
| manufacturer = Objects.toString(result.getValue(BiosProperty.Manufacturer, 0)); | |
| // | |
| } // for | |
| // | |
| return manufacturer; | |
| // | |
| } finally { | |
| // | |
| if (ole32 != null) { | |
| // | |
| ole32.CoUninitialize(); | |
| // | |
| } // if | |
| // | |
| } // try | |
| // | |
| } else if (Objects.equals(name, "sun.nio.fs.MacOSXFileSystem")) { | |
| // | |
| final Process process = new ProcessBuilder("ioreg", "-l", "-p", "IODeviceTree").start(); | |
| // | |
| String firmwareVendor = null; | |
| // | |
| try (final InputStream is = process != null ? process.getInputStream() : null) { | |
| // | |
| final Collection<String> lines = IOUtils.readLines(is, StandardCharsets.UTF_8); | |
| // | |
| if (lines != null) { | |
| // | |
| Pattern pattern = null; | |
| // | |
| Matcher matcher = null; | |
| // | |
| for (final String line : lines) { | |
| // | |
| if (pattern == null) { | |
| // | |
| pattern = ObjectUtils.getIfNull(pattern, | |
| () -> Pattern.compile("^.+\\\"firmware\\-vendor\"\\s+\\=\\s+\\<([0-9a-f]+)\\>$")); | |
| // | |
| } // if | |
| // | |
| if (line != null && pattern != null && (matcher = pattern.matcher(line)) != null | |
| && matcher.matches() && matcher.groupCount() > 0) { | |
| // | |
| if (firmwareVendor != null) { | |
| // | |
| throw new IllegalStateException(); | |
| // | |
| } // if | |
| // | |
| firmwareVendor = new String(hexToBytes(matcher.group(1))); | |
| // | |
| } // if | |
| // | |
| } // for | |
| // | |
| } // if | |
| // | |
| return StringUtils.trim(firmwareVendor); | |
| // | |
| } // try | |
| // | |
| } // if | |
| // | |
| return null; | |
| // | |
| } | |
| private static byte[] hexToBytes(final String hexString) { | |
| // | |
| final int len = StringUtils.length(hexString); | |
| // | |
| final byte[] data = new byte[len / 2]; | |
| // | |
| for (int i = 0; i < len; i += 2) { | |
| // | |
| data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) | |
| + Character.digit(hexString.charAt(i + 1), 16)); | |
| // | |
| } // for | |
| // | |
| return data; | |
| // | |
| } | |
| private static String getName(final Class<?> instance) { | |
| return instance != null ? instance.getName() : null; | |
| } | |
| private static Class<?> getClass(final Object instance) { | |
| return instance != null ? instance.getClass() : null; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!--https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform--> | |
| <dependency> | |
| <groupId>net.java.dev.jna</groupId> | |
| <artifactId>jna-platform</artifactId> | |
| <version>5.18.1</version> | |
| </dependency> | |
| <!--https://mvnrepository.com/artifact/org.apache.commons/commons-lang3--> | |
| <dependency> | |
| <groupId>org.apache.commons</groupId> | |
| <artifactId>commons-lang3</artifactId> | |
| <version>3.20.0</version> | |
| </dependency> | |
| <!--https://mvnrepository.com/artifact/commons-io/commons-io--> | |
| <dependency> | |
| <groupId>commons-io</groupId> | |
| <artifactId>commons-io</artifactId> | |
| <version>2.22.0</version> | |
| </dependency> |
Author
Output
| Virtual or Physical | Result | ||
|---|---|---|---|
| Virtual Box | Virtual | innotek GmbH | https://github.com/VirtualBox/virtualbox/blob/main/src/VBox/Devices/PC/DevFwCommon.cpp#L69C1-L69C66
|
| HP HP EliteBook 830 G7 Notebook PC | Physical | HP | |
| Online Java Compiler - Run Java Code in Browser | JDoodle | DigitalOcean |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Uh oh!
There was an error while loading. Please reload this page.