无法引入import com.sun.management.OperatingSystemMXBean

本文介绍了解决Eclipse中无法导入受访问限制API的问题,例如com.sun.management.OperatingSystemMXBean。通过调整Eclipse的编译器设置,可以将这些受限API的错误级别从ERROR改为WARNING,从而实现成功编译。

现象:在JDK的安装包的jre\lib\rt.jar包里确实有这个类com.sun.management.OperatingSystemMXBean,但是就是不能import  com.sun.management.OperatingSystemMXBean

 

答案:

Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。

转载于:https://www.cnblogs.com/snake-hand/p/3146782.html

package com.xdas.common.core.jvm; import com.sun.tools.attach.VirtualMachine; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; public class Assist { static { try { loadNativeLibrary("JvmAssist"); } catch (Exception e) { throw new RuntimeException(e); } } public static void loadDllFromResources(String dllName) throws Exception { // 获取 DLL 文件的资源流 InputStream dllStream = Assist.class.getClassLoader().getResourceAsStream(dllName); if (dllStream == null) { throw new Exception("DLL file not found in resources: " + dllName); } // 创建临时文件并写入 DLL 内容 File tempDllFile = new File(System.getProperty("java.io.tmpdir"), dllName); try (FileOutputStream fos = new FileOutputStream(tempDllFile)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = dllStream.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } } System.load(tempDllFile.getAbsolutePath()); System.out.println("loaded successfully from resources: " + dllName); } public static void copyFileUsingFileStreams(InputStream input, File dest) throws Exception { OutputStream output = null; try { output = new FileOutputStream(dest); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } } finally { input.close(); output.close(); } } private static boolean loadNativeLibrary(String libName) { String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; try { VirtualMachine vm = VirtualMachin
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值