javajarexelaunch4jsigar

SIGAR API not working after packaging to jar/exe


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

Solution

  • 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());