Java 为什么OperatingSystemMxBean 的访问仅限于jre6/lib/rt.jar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4183864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is access restricted to jre6/lib/rt.jar for OperatingSystemMxBean?
提问by Mister Dai
I'm having a little trouble with some Java code I'm trying to compile in Eclipse. I keep getting the following warning...
我试图在 Eclipse 中编译一些 Java 代码时遇到了一些麻烦。我不断收到以下警告...
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?
我找到了解决方法,但我担心限制警告。此代码适用于我的开源项目 ( CfTracker),如果我要违反某种许可协议,我不想解决此限制。任何人都可以帮助我理解这一点吗?
采纳答案by Thilo
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。
回答by Zohaib
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.)
转到窗口--> 首选项--> Java--> 编译器--> 错误/警告。选择弃用和受限 API。将其更改为警告。更改禁止和不鼓励的参考并将其更改为警告。(或根据您的需要。)
Thanks.
谢谢。
回答by Shivam Verma
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.
转到项目属性中的构建路径设置。
删除 JRE 系统库
加回来;选择“添加库”并选择 JRE 系统库。
回答by IvanRF
Here it is explained Why Developers Should Not Write Programs That Call 'sun' Packages.
这里解释了为什么开发人员不应该编写调用“sun”包的程序。
A workaround used by OrientDB hereis 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) {
}