23

How to get the Ram size and Hard disk size of the PC using Java? And Is it possible to get the currently logged user name on PC through java?

Kara
  • 6,115
  • 16
  • 50
  • 57
bharath
  • 14,283
  • 16
  • 57
  • 95
  • 1
    Related : http://stackoverflow.com/questions/25552/using-java-to-get-os-level-system-information – jmj Apr 01 '11 at 10:59

5 Answers5

29

Disk size:

long diskSize = new File("/").getTotalSpace();

User name:

String userName = System.getProperty("user.name");

I'm not aware of a reliable way to determine total system memory in Java. On a Unix system you could parse /proc/meminfo. You can of course find the maximum memory available to the JVM:

long maxMemory = Runtime.getRuntime().maxMemory();

Edit: for completeness (thanks Suresh S), here's a way to get total memory with the Oracle JVM only:

long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory
        .getOperatingSystemMXBean()).getTotalPhysicalMemorySize();
WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
  • I find very useful for auditing purposes (i.e. logging who did what e.g. I pass it into RMI calls), but its not very secure. – Peter Lawrey Apr 01 '11 at 11:37
  • 4
    "Oracle JVM only" is **not** correct, as of JDK 1.8 this is an jdk.Exported type, see http://docs.oracle.com/javase/8/docs/jdk/api/javac/tree/jdk/Exported.html – rmuller Oct 31 '14 at 12:11
  • The disk size refers to the drive size not, the physical disk. –  Nov 08 '18 at 05:13
7

For Ram Size , if you are using java 1.5

java.lang.management package

com.sun.management.OperatingSystemMXBean mxbean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
System.out.println(mxbean.getTotalPhysicalMemorySize() + " Bytes "); 
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
5
import java.lang.management.*;
import java.io.*;

class max
{
    public static void main(String... a)
    {
        long diskSize = new File("/").getTotalSpace();
        String userName = System.getProperty("user.name");
        long maxMemory = Runtime.getRuntime().maxMemory();
        long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize();
        System.out.println("Size of C:="+diskSize+" Bytes");
        System.out.println("User Name="+userName);

        System.out.println("RAM Size="+memorySize+" Bytes");
   }
}
CuberChase
  • 4,458
  • 5
  • 33
  • 52
Atin Agrawal
  • 178
  • 3
  • 12
3

Have a look at this topic, which goes into detail of how to get OS information such as this.

Community
  • 1
  • 1
Andreas Johansson
  • 1,135
  • 6
  • 23
1

For Ram capacity: //this step get ram capacity

long ram= ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize();

            long sizekb = ram /1000;
    long sizemb = sizekb / 1000;
    long sizegb = sizemb / 1000 ;
    System.out.println("System Ram ="+sizegb+"gb");
Arun Kumar
  • 11
  • 1