issue when accessing oshi library in jsp

577 Views Asked by At

I am getting the problem is grabbing details through JSP. I am using following versions: jna-4.2.2.jar, slf4j-api-1.7.21.jar, slf4j-simple-1.7.21.jar, jna-platform-4.2.2.jar, oshi-core-2.3.jar The problem is whenever I execute my below code from JSP I get the errors stated below, but in normal Java App it works fine without any error?

Is this a bug with JSP? or Is there any other way to call it in JSP?

<%@page import="oshi.hardware.HardwareAbstractionLayer"%>
<%@page import="oshi.software.os.OperatingSystem"%>
<%@page import="oshi.SystemInfo"%>
<%
    SystemInfo si = new SystemInfo();
    OperatingSystem os = si.getOperatingSystem();
     HardwareAbstractionLayer hal = si.getHardware();
     out.println(hal.getProcessor());
%>

and the error is:

Warning:   StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NoSuchMethodError: com.sun.jna.IntegerType.<init>(IJZ)V
    at com.sun.jna.platform.win32.WinDef$DWORD.<init>(WinDef.java:135)
    at com.sun.jna.platform.win32.WinDef$DWORD.<init>(WinDef.java:125)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at java.lang.Class.newInstance(Class.java:442)
    at com.sun.jna.NativeMappedConverter.defaultValue(NativeMappedConverter.java:47)
    at com.sun.jna.NativeMappedConverter.<init>(NativeMappedConverter.java:41)
    at com.sun.jna.NativeMappedConverter.getInstance(NativeMappedConverter.java:29)
    at com.sun.jna.Structure.calculateSize(Structure.java:717)
    at com.sun.jna.Structure.allocateMemory(Structure.java:251)
    at com.sun.jna.Structure.<init>(Structure.java:153)
    at com.sun.jna.Structure.<init>(Structure.java:143)
    at com.sun.jna.Structure.<init>(Structure.java:139)
    at com.sun.jna.Structure.<init>(Structure.java:130)
    at com.sun.jna.platform.win32.WinBase$MEMORYSTATUSEX.<init>(WinBase.java:595)
    at oshi.hardware.platform.windows.WindowsGlobalMemory.<init>(WindowsGlobalMemory.java:40)
    at oshi.hardware.platform.windows.WindowsHardwareAbstractionLayer.getMemory(WindowsHardwareAbstractionLayer.java:50)
    at org.apache.jsp.api.cpuload_jsp._jspService(cpuload_jsp.java:62)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    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:415)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
    at java.lang.Thread.run(Thread.java:745)

Please suggest some solution to the issue.

2

There are 2 best solutions below

0
On

The stack trace line numbers indicate you're using jna-platform-4.2.2 but somehow your code is finding com.sun.jna.IntegerType somewhere else than jna-4.2.2.jar.

Try running this code:

Class klass = com.sun.jna.IntegerType.class;
URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class");

Then output location.toString() and verify it's in the jna-4.2.2 jar. You should get a result like

jar:file:/ ...path to jar... /jna-4.2.2.jar!/com/sun/jna/IntegerType.class

If not, you've found the location where Java's classloader is loading the incorrect class. Then figure out how to exclude the class from that alternate location (remove the bad jar, use newer resource bundle, etc.)

1
On

It looks like you are missing two jar files

If you are using Netbeans:

Goto Project Properties > Libraries > Compile > Add JAR

Now add two files:-

javax.json-1.0.4.jar

javax.json-api-1.0.jar

You can download them from here json and json-api

For documentation reference look here: https://jsonp.java.net/download.html