2

Sorry guys I have tried this again through creating class with method inside the main class but it showing this error in jsp page ;

java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Sigar.getCpuInfoList()[Lorg/hyperic/sigar/CpuInfo;

I'm not sure if that from the code or Sigar API library sorry again but i need help to get this jsp right

package mydata;

import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class MyCpu {
private String cpuInfoList;

public String getCpuInfoList() {
 return this.cpuInfoList;
}

public MyCpu() {
 Sigar sigar = new Sigar();
 String output = " ";
 CpuInfo[] cpuInfoList = null;
 try {

  cpuInfoList = sigar.getCpuInfoList();
 } catch (SigarException e) {
  e.printStackTrace();
  return;
 }
 for (CpuInfo info : cpuInfoList) {
  output += "Vendor: " + info.getVendor() + "";
 }
 System.out.println(output);
}

public static void main(String[] args) {
 MyCpu main = new MyCpu();
}
}

--------------------------------------------------------------------------------------
  <!---JSP--->
  
  <%@page import ="org.hyperic.sigar.Sigar"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%@page import="mydata.*"%>
        <%
    MyCpu cpu = new MyCpu();
    Sigar sigar = new Sigar();
    out.println(sigar.getCpuInfoList());
%>
    </body>
</html>
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Sadigain
  • 41
  • 1
  • 5
  • This looks like an error inside the `getCpuInfoList()` method. Can you post the full stacktrace? I'd suspect that a library is missing on the classpath and thus this exception should be thrown at the line `MyCpu cpu = new MyCpu();` – Thomas May 04 '15 at 11:19
  • That is all the code and I have downloaded Sigar library and added to JRE folder, and now I can run it as java application OK BUT I can't run this from JSP . Thanks Thomas – Sadigain May 04 '15 at 11:22
  • The output should be = Vendor + " the CPU vendor name" – Sadigain May 04 '15 at 11:25
  • Are you sure that your JSP container uses that JRE? Btw, adding application depencencies to the JRE is not the common way to do that or is that a JVM plugin? – Thomas May 04 '15 at 11:26
  • That is 2 executable jar file (log4j and sigar) i have added them to libraries – Sadigain May 04 '15 at 11:31
  • Did you check this : http://stackoverflow.com/questions/11612711/sigar-unsatisfiedlinkerror – Rockstar May 04 '15 at 11:59
  • If those dependencies are just jars I'd add them to the application's classpath (where exactly depends on the packaging, e.g. for a war it would be WEB-INF/lib) in order to be independent from the JVM there. – Thomas May 04 '15 at 11:59

1 Answers1

0

You can call java code in JSP using scriplet but it's bad practice to use scriplet in jsp instead use JSTL. Lets move on create one class as mentioned below

public class Demo {
    public String output = "";

    public Demo() {
        CpuInfo[] cpuInfoList = null;
        try {
            cpuInfoList = new Sigar().getCpuInfoList();
        } catch (SigarException e) {
            e.printStackTrace();
        }
        for (CpuInfo info : cpuInfoList) {
            output += info.getVendor() + "\n";
        }
        System.out.println(output);
    }

    public static void main(String[] args) {
        new Demo();
    }
}

Change your jsp code to following

<%@page import="com.Demo"%>

<!DOCTYPE HTML">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
            out.println(new Demo().output);
        %>
    </body>
</html>

And Add this to classpath -Djava.library.path="./lib" in manifest.mf of webapp so finally menifest.mf looks like:

Manifest-Version: 1.0
Class-Path: -Djava.library.path="./lib" 

And ofcourse add sigar.jar in lib folder located in WEB_INF.

EDIT:

You should see some kind of below error in your server log:

Info: 5 [http-listener-1(5)] DEBUG Sigar  - no sigar-amd64-winnt.dll in java.library.path
org.hyperic.sigar.SigarException: no sigar-amd64-winnt.dll in java.library.path

So you have to provide sigar-amd64-winnt.dll in lib folder as well.

Lib

And now you will see output like

output

Naman
  • 2,205
  • 2
  • 19
  • 32
  • Thank You Naman for your help I will try this and come back to you – Sadigain May 04 '15 at 18:07
  • It works mates but also ignoring the main class, this data as you can see is not invoked from the class, it import from class library if I'm right. – Sadigain May 11 '15 at 18:20
  • That is very helpful mate, yes it does exactly what supposed to do through calling the method not directly accessing the lib. – Sadigain May 12 '15 at 11:11