gpt4 book ai didi

java - Sigar API 抛出 "org.hyperic.sigar.SigarPermissionDeniedException"

转载 作者:行者123 更新时间:2023-11-30 11:25:44 24 4
gpt4 key购买 nike

我是 Sigar 的新手,我正在尝试使用 Sigar API 来收集 CPU 使用率。

但是 Sigar 抛出了这个:

org.hyperic.sigar.SigarPermissionDeniedException: Access is denied.

谁能帮帮我?这是我的代码:

public void pollCheck() {
SysInfo sys=new SysInfo();
System.out.println("Total CPU:"+sys.getTotalCPU());
}

这是 SysInfo.java:

import org.hyperic.sigar.ProcCpu;
import org.hyperic.sigar.Sigar;
public class SysInfo {
public long[] getProList(){
long[] result=null;
try{
Sigar sigar=new Sigar();
result=sigar.getProcList();
}catch(Exception e){
System.out.println("Error in SysInfo.getProList:"+e.toString());
e.printStackTrace();
}
return result;
}
public double getTotalCPU(){
long[] procs=getProList();
double total=0.0;
try{
Sigar sigar=new Sigar();
for(long proc:procs){
ProcCpu temp=sigar.getProcCpu(proc);
temp.gather(sigar, proc);
total+=temp.getPercent();
}
}catch(Exception e){
System.out.println("Error in SysInfo.getTotalCPU:"+e.toString());
e.printStackTrace();
}
return total;
}
}

错误信息是:

[2013-11-25T10:53:24.651+0800] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=22 _ThreadName=Thread-3] [timeMillis: 1385348004651] [levelValue: 800] [[
Error in SysInfo.getTotalCPU:org.hyperic.sigar.SigarPermissionDeniedException: Access is denied.]]
[2013-11-25T10:53:24.655+0800] [glassfish 4.0] [SEVERE] [] [] [tid: _ThreadID=22 _ThreadName=Thread-4] [timeMillis: 1385348004655] [levelValue: 1000] [[
org.hyperic.sigar.SigarPermissionDeniedException: Access is denied.
at org.hyperic.sigar.ProcCpu.gather(Native Method)
at org.hyperic.sigar.ProcCpu.fetch(ProcCpu.java:30)
at org.hyperic.sigar.Sigar.getProcCpu(Sigar.java:488)
at bean.SysInfo.getTotalCPU(SysInfo.java:32)
at bean.indexBean.pollCheck(indexBean.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at javax.el.ELUtil.invokeMethod(ELUtil.java:326)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:536)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
at com.sun.el.parser.AstValue.invoke(AstValue.java:269)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at org.primefaces.component.poll.Poll.broadcast(Poll.java:216)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:724)]]
[2013-11-25T10:53:24.658+0800] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=22 _ThreadName=Thread-3] [timeMillis: 1385348004658] [levelValue: 800] [[
Total CPU:0.0]]

抱歉我的英语不好。

提前致谢。

最佳答案

我想我知道为什么...

操作系统不允许你的程序访问进程信息,直到你以管理员权限运行程序,所以我需要提供管理员权限。

有一些方法可以提供管理员权限,包括要求用户右键单击图标并选择“以管理员身份运行”。

但更好的方法可能是将您的 Java 应用程序包装在 .exe 中,例如 herehere说。

但是,我可能不得不像 excowboy 所说的那样破解操作系统 here因为我想在网页上显示信息并且无法将我的应用程序包装在 .exe 文件中。

我想我不得不放弃了。只需将这些链接提供给可能需要它的人。 :)哦,顺便说一句,如果您愿意,总的 CPU 使用率是可以访问的。

关于java - Sigar API 抛出 "org.hyperic.sigar.SigarPermissionDeniedException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20184353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com