简体   繁体   English

为什么对于OperatingSystemMxBean,访问仅限于jre6 / lib / rt.jar?

[英]Why is access restricted to jre6/lib/rt.jar for OperatingSystemMxBean?

I'm having a little trouble with some Java code I'm trying to compile in Eclipse. 我正在尝试在Eclipse中编译的一些Java代码遇到一些麻烦。 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. 这段代码适用于我的开源项目( CfTracker ),如果我打算违反某种许可协议,我不想解决这个限制。 Can anyone help me understand this? 任何人都可以帮我理解这个吗?

Go to Window-->Preferences-->Java-->Compiler-->Error/Warnings. 转到窗口 - >首选项 - > Java - >编译器 - >错误/警告。 Select Deprecated and Restricted API. 选择已弃用和受限制的API。 Change it to warning. 将其更改为警告。 Change forbidden and Discouraged Reference and change it to warning. 更改禁止和气馁参考并将其更改为警告。 (or as your need.) (或根据您的需要。)

Thanks. 谢谢。

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). 只是Eclipse试图保护您不使用不属于官方JDK API的类(而是Oracle / Sun的JVM实现的一部分)。

Is there a particular reason that you need to class cast (rather than using the "official" interface java.lang.management.OperatingSystemMXBean )? 是否有特殊原因需要进行类转换(而不是使用“官方”接口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. 如果要确保在预期的MXBean不可用时继续运行应用程序,可以添加一些try / catch逻辑以正常处理ClassCastException。

The best solution i found for this one is : 我找到的最佳解决方案是:

  • Go to the Build Path settings in the project properties. 转到项目属性中的“构建路径”设置。

  • Remove the JRE System Library 删除JRE系统库

  • Add it back; 加回来; Select "Add Library" and select the JRE System Library. 选择“添加库”并选择JRE系统库。

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

Here it is explained Why Developers Should Not Write Programs That Call 'sun' Packages . 这里解释了为什么开发人员不应该编写称为“sun”软件包的程序

A workaround used by OrientDB here is reflection. 使用OrientDB一种解决方法在这里是反思。

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) {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM