1

I am trying to gather system information using the SIGAR API. After including sigar.jar in my library, Netbeans gave the error that sigar-amd64-winnt.dll was not found. After adding that file to the library, it worked perfectly.

Now i needed to create an executable jar so I used NetBeans to Build my project. Netbeans automatically deleted the .dll file and after i manually copied it back to the dist folder, my program worked. I am now trying to create an exe file using Launch4j. When i run it, a java exception is thrown, probably because Launch4j deleted the .dll file.

How can i make sure Launch4j includes it in the exe file?

I browsed through numerous answers on SO but couldnt solve my problem. Have mentioned some below-

sigar-amd64-winnt.dll ... can't reference it or bundle it with .jar

How to include SIGAR API in Java Project

Any help would be appreciated..

EDIT

Launch4j gives the following Exception in its log when i test run the exe wrapper-

Exception in thread "main" java.lang.NoClassDefFoundError: org/hyperic/sigar/SigarException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.hyperic.sigar.SigarException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
Community
  • 1
  • 1
Pranav
  • 323
  • 1
  • 3
  • 16

2 Answers2

1

Okay, so in order to make sure that the program works, we need to copy the native .dll library onto the computer from the packaged exe(put the required dll libraries into the src folder) and then load it as a library.

This is what finally worked for me-

int arch = Integer.parseInt(System.getProperty("sun.arch.data.model"));
    InputStream is = null;
    if(arch==32)
        is = Logger.class.getClass().getResourceAsStream("/sigar-x86-winnt.dll");
    else if(arch==64)
        is = Logger.class.getClass().getResourceAsStream("/sigar-amd64-winnt.dll");

    Path sigar = Files.createTempFile("sigar_lib", ".dll");

    try (FileOutputStream out = new FileOutputStream(sigar.toFile())) 
            {
                byte[] buffer = new byte[1024];
                int len;
                while ((len = is.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
            } 
            catch (Exception e) {

        }
    System.load(sigar.toString());
Pranav
  • 323
  • 1
  • 3
  • 16
  • But there is a problem which every time it will create a new file in the temp folder, if you run the program for many times, the C drive will become full day by day. 1Mb for 3 run – Bahramdun Adil Jun 03 '15 at 09:48
  • `sigar.deleteOnExit();` will make sure the file is deleted when the JVM terminates. – Pranav Aug 21 '15 at 07:51
0

Specify the .dll path if it is Windows or else specify .so if it is Ubuntu. For example:

java -Djava.library.path=".:/home/ubuntu/AssetManager/libsigar-amd64-linux.so" -jar aa.jar

Here .:/home/ubuntu/AssetManager/libsigar-amd64-linux.so is the path of the Sigar API .so file

aa.jar is the actual runnable jar created by Eclipse

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197