Saturday, 27 February 2016

Getting System Hardware Information Using Java (only windows)

Save your precious data
After searching on the web I found that there is no any pure Java code to get all system hardware information. Although we can get some system details using Java like processor, system architecture and the number of processors using the code below:-

System.out.println(System.getenv("PROCESSOR_IDENTIFIER")); System.out.println(System.getenv("PROCESSOR_ARCHITECTURE")); System.out.println(System.getenv("PROCESSOR_ARCHITEW6432")); System.out.println(System.getenv("NUMBER_OF_PROCESSORS"));

 But beyond this if we want all hardware associated to system like hard disk, RAM, CD/DVD W/ROM network card, BIOS, monitor and other. Then there is no direct code like mentioned above is not available. Either you have to use some third party API or windows tools to get system information.

Using other APIs is sometimes complex to use in our code and configuration is not easy. You can use API like SIGAR which I have heard most.
You can visit it here and can download from here.
But it is also limited.

 In the second approach, you can use windows tool in your Java program to get detailed information of system hardware, which is known as WMIC(Windows Management Instrumentation Command-line).
The sample code is given below to get some system hardware and software information.

String[][] commands = new String[][]
{
 {"CMD", "/C", "WMIC OS GET Installdate,SerialNumber"},
 //OS Installation date & Time, OS Serial Number
 {"CMD", "/C", "WMIC BASEBOARD GET SerialNumber"},
 // Motherboadrd Serial Number 
 {"CMD", "/C", "WMIC CPU GET ProcessorId"}, 
// Processor ID 
 {"CMD", "/C", "WMIC COMPUTERSYSTEM GET Name"},
// Computer Name
 {"CMD", "/C", "WMIC diskdrive GET Name, Manufacturer, Model, InterfaceType, MediaLoaded, MediaType"},
//HDD Details 
 {"CMD", "/C", "WMIC memphysical GET Manufacturer, Model, SerialNumber, MaxCapacity, MemoryDevices"}, 
 }; 

 for (int i = 0; i < commands.length; i++)
 {

 String[] com = commands[i];
 Process process = Runtime.getRuntime().exec(com);
 process.getOutputStream().close();
 //Closing output stream of the process
 System.out.println();
 String s = null;
 //Reading sucessful output of the command
 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 

 while ((s = reader.readLine()) != null) 

 System.out.print(s);
 }

 // Reading error if any 
 reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));

 while ((s = reader.readLine()) != null)
 { 
 System.out.print(s); 
 }

 }

 Detailed about WMIC you can visit here.

Some other useful commands given below:-

baseboard get Manufacturer, Model, Name, PartNumber, slotlayout, serialnumber, poweredon

bios get name, version, serialnumber

bootconfig get BootDirectory, Caption, TempDirectory, Lastdrive

cdrom get Name, Drive, Volumename

computersystem get Name, domain, Manufacturer, Model, NumberofProcessors, PrimaryOwnerName,Username, Roles, totalphysicalmemory

cpu get Name, Caption, MaxClockSpeed, DeviceID, status

datafile where name='c:\\boot.ini' get Archive, FileSize, FileType, InstallDate, Readable, Writeable, System, Version

dcomapp get Name, AppID /format:list desktop get Name, ScreenSaverExecutable, ScreenSaverActive, Wallpaper/format:list

desktopmonitor get screenheight, screenwidth

diskdrive get Name, Manufacturer, Model, InterfaceType, MediaLoaded, MediaType

diskquota get User, Warninglimit, DiskSpaceUsed, QuotaVolume

environment get Description, VariableValue

fsdir where name='c:\\windows' get Archive, CreationDate, LastModified, Readable, Writeable, System, Hidden, Status

group get Caption, InstallDate, LocalAccount, Domain, SID, Status

idecontroller get Name, Manufacturer, DeviceID, Status

irq get Name, Status

job get Name, Owner, DaysOfMonth, DaysOfWeek, ElapsedTime, JobStatus, StartTime, Status

loadorder get Name, DriverEnabled, GroupOrder, Status

logicaldisk get Name, Compressed, Description, DriveType, FileSystem,FreeSpace, SupportsDiskQuotas, VolumeDirty, VolumeName

memcache get Name, BlockSize, Purpose, MaxCacheSize, Status

memlogical get AvailableVirtualMemory, TotalPageFileSpace,TotalPhysicalMemory, TotalVirtualMemory

memphysical get Manufacturer, Model, SerialNumber, MaxCapacity,MemoryDevices

netclient get Caption, Name, Manufacturer, Status

netlogin get Name, Fullname, ScriptPath, Profile, UserID, NumberOfLogons,PasswordAge, LogonServer, HomeDirectory, PrimaryGroupID

netprotocol get Caption, Description, GuaranteesSequencing,SupportsBroadcasting, SupportsEncryption, Status

netuse get Caption, DisplayType, LocalName, Name, ProviderName, Status

nic get AdapterType, AutoSense, Name, Installed, MACAddress,PNPDeviceID,PowerManagementSupported, Speed, StatusInfo

nicconfig get MACAddress, DefaultIPGateway, IPAddress, IPSubnet, DNSHostName, DNSDomain

nicconfig get MACAddress, IPAddress, DHCPEnabled, DHCPLeaseExpires, DHCPLeaseObtained, DHCPServer

nicconfig get MACAddress, IPAddress, DNSHostName, DNSDomain, DNSDomainSuffixSearchOrder, DNSEnabledForWINSResolution, DNSServerSearchOrder

nicconfig get MACAddress, IPAddress, WINSPrimaryServer, WINSSecondaryServer, WINSEnableLMHostsLookup, WINSHostLookupFile

ntdomain get Caption, ClientSiteName, DomainControllerAddress, DomainControllerName, Roles, Status

ntevent where (LogFile='system' and SourceName='W32Time') get Message, TimeGenerated

ntevent where (LogFile='system' and SourceName='W32Time' and Message like '%timesource%') get Message, TimeGenerated

ntevent where (LogFile='system' and SourceName='W32Time' and EventCode!='29') get TimeGenerated, EventCode, Message

onboarddevice get Description, DeviceType, Enabled, Status

os get Version, Caption, CountryCode, CSName, Description, InstallDate, SerialNumber, ServicePackMajorVersion, WindowsDirectory /format:list

os get CurrentTimeZone, FreePhysicalMemory, FreeVirtualMemory, LastBootUpTime, NumberofProcesses, NumberofUsers, Organization, RegisteredUser, Status

pagefile get Caption, CurrentUsage, Status, TempPageFile

pagefileset get Name, InitialSize, MaximumSize

partition get Caption, Size, PrimaryPartition, Status, Type

printer get DeviceID, DriverName, Hidden, Name, PortName, PowerManagementSupported, PrintJobDataType, VerticalResolution, Horizontalresolution

printjob get Description, Document, ElapsedTime, HostPrintQueue, JobID, JobStatus, Name, Notify, Owner, TimeSubmitted, TotalPages

process get Caption, CommandLine, Handle, HandleCount, PageFaults, PageFileUsage, PArentProcessId, ProcessId, ThreadCount

product get Description, InstallDate, Name, Vendor, Version

qfe get description, FixComments, HotFixID, InstalledBy, InstalledOn, ServicePackInEffect

quotasetting get Caption, DefaultLimit, Description, DefaultWarningLimit, SettingID, State

recoveros get AutoReboot, DebugFilePath, WriteDebugInfo, WriteToSystemLog

Registry get CurrentSize, MaximumSize, ProposedSize, Status

scsicontroller get Caption, DeviceID, Manufacturer, PNPDeviceID

server get ErrorsAccessPermissions, ErrorsGrantedAccess, ErrorsLogon, ErrorsSystem, FilesOpen, FileDirectorySearches

service get Name, Caption, State, ServiceType, StartMode, pathname

share get name, path, status

sounddev get Caption, DeviceID, PNPDeviceID, Manufacturer, status

startup get Caption, Location, Command sysaccount get Caption, Domain, Name, SID, SIDType, Status

sysdriver get Caption, Name, PathName, ServiceType, State, Status

systemenclosure get Caption, Height, Depth, Manufacturer, Model, SMBIOSAssetTag, AudibleAlarm, SecurityStatus, SecurityBreach, PoweredOn, NumberOfPowerCords

systemslot get Number, SlotDesignation, Status, SupportsHotPlug, Version, CurrentUsage, ConnectorPinout

tapedrive get Name, Capabilities, Compression, Description, MediaType, NeedsCleaning, Status, StatusInfo

timezone get Caption, Bias, DaylightBias, DaylightName, StandardName

useraccount get AccountType, Description, Domain, Disabled, LocalAccount, Lockout, PasswordChangeable, PasswordExpires, PasswordRequired, SID

memorychip get BankLabel, Capacity, Caption, CreationClassName, DataWidth, Description, Devicelocator, FormFactor, HotSwappable, InstallDate, InterleaveDataDepth, InterleavePosition, Manufacturer, MemoryType, Model, Name, OtherIdentifyingInfo, PartNumber, PositionInRow, PoweredOn, Removable, Replaceable,SerialNumber, SKU, Speed, Status, Tag, TotalWidth, TypeDetail, Version

No comments:

Post a Comment

Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...