Java get available memory ( eclipse )

I need to get the total ram size of the computer using java. I've tried this:

package test;

import java.lang.management.ManagementFactory;

public class test1 {

    public static void main(String[] args) {

        com.sun.management.OperatingSystemMXBean mxbean = 
            (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

        float a = mxbean.getTotalPhysicalMemorySize();
        System.out.println(a + " bytes");

    }
}

And I have an error:

Access restriction: The type OperatingSystemMXBean is not accessible due to restriction on required library /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/rt.jar


This is just Eclipse trying to discourage you from using the internal com.sun packages. You can make the error go away by changing your preferences:

Preferences
 > Java
  > Compiler
   > Errors/Warnings
    > Deprecated and restricted API
     > Forbidden reference (access rules)

Change this value to Warning or Ignore, but make sure to read up on It is a bad practice to use Sun's proprietary Java classes? to understand what you are doing.

链接地址: http://www.djcxy.com/p/73838.html

上一篇: 是`=`和`<

下一篇: Java获得可用内存(eclipse)