2016-12-10 52 views
-1

我需要使用java獲得計算機的總RAM大小。我已經試過這樣:Java獲得可用內存(eclipse)

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"); 

    } 
} 

而且我有一個錯誤:

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

回答

0

這僅僅是Eclipse的試圖從使用內部com.sun包你泄氣。你可以使誤差通過改變自己的喜好走:

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

更改此值以警告或忽略,但一定要在It is a bad practice to use Sun's proprietary Java classes?讀取多達瞭解你在做什麼。

+0

不錯! thx老兄! :D現在都在工作 – starkmax