16

I'm having a little trouble with some Java code I'm trying to compile in Eclipse. I keep getting the following warning...

Access restriction: The type OperatingSystemMXBean is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar

From this line of code...

com.sun.management.OperatingSystemMXBean bean = (com.sun.management.OperatingSystemMXBean) java.lang.management.ManagementFactory.getOperatingSystemMXBean();

I've found ways around this but I'm worried about the restriction warning. This code is for my open source project (CfTracker) and I don't want to work around this restriction if I'm going to be breaking some sort of license agreement. Can anyone help me understand this?

Mister Dai
  • 796
  • 1
  • 6
  • 19

4 Answers4

16

Go to Window-->Preferences-->Java-->Compiler-->Error/Warnings. Select Deprecated and Restricted API. Change it to warning. Change forbidden and Discouraged Reference and change it to warning. (or as your need.)

Thanks.

Zohaib
  • 417
  • 9
  • 24
14

This is not a problem of license agreements. It is just Eclipse trying to protect you from using classes that are not part of the official JDK API (but rather, part of Oracle/Sun's JVM implementation).

Is there a particular reason that you need to class cast (rather than using the "official" interface java.lang.management.OperatingSystemMXBean)?

If you want to make sure that your application continues to run when the expected MXBean is not available, you could add some try/catch logic to gracefully handle a ClassCastException.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Thanks Thilo. That clears it up for me. I was casting it to get the getProcessCpuTime() method that's in the Oracle/Sun implementation. I'm new to Java so didn't know it wasn't an offical API. I'll look into using a try catch though as that makes a lot of sense if it's not going to be there all the time :) cheers! – Mister Dai Nov 15 '10 at 12:18
  • I think it's better to override this option on the project settings level. – user626528 Mar 24 '15 at 06:56
  • @user626528, how can we do this override in project settings level to make use of this? – Sunil N Apr 06 '22 at 15:33
9

The best solution i found for this one is :

  • Go to the Build Path settings in the project properties.

  • Remove the JRE System Library

  • Add it back; Select "Add Library" and select the JRE System Library.

https://stackoverflow.com/a/2174607/1572446

Community
  • 1
  • 1
Shivam Verma
  • 426
  • 4
  • 10
  • I just stumbled upon this trying to fix my problem and this worked. Super easy and no messy special-circumstance configuring. Thanks! – RNGuy Nov 04 '15 at 14:05
4

Here it is explained Why Developers Should Not Write Programs That Call 'sun' Packages.

A workaround used by OrientDB here is reflection.

As an example:

try {
    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    if (Class.forName("com.sun.management.OperatingSystemMXBean").isInstance(os)) {
        Method memorySize = os.getClass().getDeclaredMethod("getTotalPhysicalMemorySize");
        memorySize.setAccessible(true);
        return (Long) memorySize.invoke(os);
    }
} catch (Exception e) {
}
IvanRF
  • 7,115
  • 5
  • 47
  • 71