似乎无法在java Web Start中加载dll

时间:2014-09-23 14:40:17

标签: netbeans dll java-native-interface java-web-start sigar

我的java webstart应用程序遇到了很大问题,我尝试了很多解决方案,但最终似乎都没有正常工作。

我需要编写一个webstart applet来加载有关客户端计算机的基本硬件信息,以检查我的客户端是否可以在我们的系统上连接并使用我们的四个软件课程。我使用Sigar加载CPU和内存信息,然后使用JNI加载一个自定义的c ++脚本来检查显卡名称(这个完美无缺)。

我已将所有dll放在src / resources文件夹中以将其加载到jar中,我也使用我们在此处调用的内容"引擎"哪些是进行指定操作的分类(在我们的例子中,Jni Engine,配置引擎和数据引擎(下面的代码)),我是webstart的新手,所以我不确定这个概念是否适用于库加载

我尝试将jar中的dll作为Netbeans中的库添加,我尝试在jnlp中添加dll,但每次运行都会重新创建它,我无法添加它们有了项目属性,我终于以一种方式构建了我的数据引擎,它应该在java临时目录中加载dll,以防它们不存在,但Sigar仍然不想工作。我还将我的dll正确配置在java.library.path中(因为它在本地工作)。

当我在本地运行我的主类时(右键单击运行),但当我单击运行按钮加载webstart时,它会崩溃并显示此错误消息(它在ConfigEngine中发生,因为它扩展了SigarBase):< / p>

  

JNLPClassLoader:查找库sigar-amd64-winnt.dll.dll   java.library.path中没有sigar-amd64-winnt.dll   org.hyperic.sigar.SigarException:java.library.path中没有sigar-amd64-winnt.dll

以下是代码:

JNi Engine(加载图形卡的c ++代码)

package Engine;
public class JniEngine
{
    static private final String nomLibJni = "JniEngine";
    static private final String nomLibJni64 = "JniEngine_x64";

    static
    {
        if (System.getProperty("os.arch").contains("86"))
        {
            System.loadLibrary(nomLibJni);
        }
        else
        {
            System.loadLibrary(nomLibJni64);
        }
    }

    public native String getInfoGPU() throws Error;
}

ConfigEngine

package Engine;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.cmd.SigarCommandBase;

public class ConfigEngine extends SigarCommandBase
{
    private final String nomOsAcceptes = "Windows";

    static
    {
        DataEngine data;
    }

    public ConfigEngine()
    {
        super();
    }

    @Override
    public void output(String[] args) throws SigarException
    {
    }

    public HashMap<String, String> getMap() throws SigarException, SocketException
    {
        HashMap<String, String> hmConfig = new HashMap<>();
        loadInfoCpu(hmConfig);
        loadInfoRam(hmConfig);
        loadInfoOs(hmConfig);
        loadInfoNet(hmConfig);
        loadInfoGpu(hmConfig);
        return hmConfig;
    }

    private void loadInfoCpu(HashMap<String,String> Hashmap) throws SigarException
    {
        org.hyperic.sigar.CpuInfo[] configCpu = this.sigar.getCpuInfoList();
        org.hyperic.sigar.CpuInfo infoCpu = configCpu[0];
        long cacheSize = infoCpu.getCacheSize();
        Hashmap.put("Builder", infoCpu.getVendor());
        Hashmap.put("Model" , infoCpu.getModel());
        Hashmap.put("Mhz", String.valueOf(infoCpu.getMhz()));
        Hashmap.put("Cpus nbr", String.valueOf(infoCpu.getTotalCores()));
        if ((infoCpu.getTotalCores() != infoCpu.getTotalSockets()) || 
                (infoCpu.getCoresPerSocket() > infoCpu.getTotalCores()))
        {
            Hashmap.put("Cpus", String.valueOf(infoCpu.getTotalSockets()));
            Hashmap.put("Core", String.valueOf(infoCpu.getCoresPerSocket()));
        }
        if (cacheSize != Sigar.FIELD_NOTIMPL) {
            Hashmap.put("Cache", String.valueOf(cacheSize));
        }
    }

    private void loadInfoRam(HashMap<String,String> Hashmap) throws SigarException
    {
        org.hyperic.sigar.Mem mem = this.sigar.getMem();
        Hashmap.put("RAM" , String.valueOf(mem.getRam()));
        Hashmap.put("Memoery", String.valueOf(mem.getTotal()));
        Hashmap.put("Free", String.valueOf(mem.getUsed()));
    }

    private void loadInfoOs(HashMap<String,String> Hashmap) throws SigarException
    {
        Hashmap.put("OS", System.getProperty("os.name"));
        Hashmap.put("Version", System.getProperty("os.version"));
        Hashmap.put("Arch", System.getProperty("os.arch"));
    }

    private void loadInfoNet(HashMap<String,String> Hashmap) throws SocketException
    {
        List<NetworkInterface> interfaces = Collections.
                                    list(NetworkInterface.getNetworkInterfaces());
        int i = 1;
        for (NetworkInterface net : interfaces)
        {
            if (!net.isVirtual() && net.isUp())
            {
                Hashmap.put("Port Name " + String.valueOf(i), net.getDisplayName());
            }
            i++;
        }
    }

    private void loadInfoGpu(HashMap<String,String> Hashmap) throws SocketException
    {
        if (System.getProperty("os.name").contains(nomOsAcceptes))
        {
            JniEngine Jni = new JniEngine();
            Hashmap.put("VGA", Jni.getInfoGPU());
        }
    }
}

最后我的数据引擎尝试加载所有dll并更改库路径(大多数都是临时的,因为它是补丁上的补丁)

package Engine;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;

public class DataEngine
{
    static private final String nomLibSigar = "sigar-x86-winnt";
    static private final String nomLibSigar64 = "sigar-amd64-winnt";
    static private final String nomLibJni = "JniEngine";
    static private final String nomLibJni64 = "JniEngine_x64";
    static private final String NomJar86 = "lib_config_x86";
    static private final String nomJar64 = "lib_config_x64";
    static private final String path = "Resources\\";

    static
    {
        try
        {
            if (System.getProperty("os.arch").contains("86"))
            {
                System.loadLibrary(nomLibJni);
                System.loadLibrary(nomLibSigar);
            }
            else
            {
                System.loadLibrary(nomLibJni64);
                System.loadLibrary(nomLibSigar64);
            }
        }
        catch (UnsatisfiedLinkError ex)
        {
            loadJniFromJar();
            loadSigarFromJar();
        }
    }

    public static void loadSigarFromJar()
    {
        try
        {
            File dll;
            InputStream is;
            if (System.getProperty("os.arch").contains("86"))
            {
                is = DataEngine.class.getResourceAsStream(
                        path + nomLibSigar + ".dll");
                dll = File.createTempFile(path + nomLibSigar, ".dll");
            }
            else
            {
                is = DataEngine.class.getResourceAsStream(
                        path + nomLibSigar64 + ".dll");
                dll = File.createTempFile(path + nomLibSigar64, ".dll");
            }
            FileOutputStream fos = new FileOutputStream(dll);
            byte[] array = new byte[1024];
            for (int i = is.read(array);
                    i != -1;
                    i = is.read(array))
            {
                fos.write(array, 0, i);
            }
            fos.close();
            is.close();
            System.load(dll.getAbsolutePath());
            System.setProperty("java.library.path", dll.getAbsolutePath()); 
        }
        catch (Throwable e)
        {
        }
    }

    public static void loadJniFromJar()
    {
        try
        {
            File dll;
            InputStream is;
            if (System.getProperty("os.arch").contains("86"))
            {
                is = DataEngine.class.getResourceAsStream(
                        path + nomLibJni + ".dll");
                dll = File.createTempFile(path + nomLibJni, ".dll");
            }
            else
            {
                is = DataEngine.class.getResourceAsStream(
                        path + nomLibJni64 + ".dll");
                dll = File.createTempFile(path + nomLibJni64, ".dll");
            }
            FileOutputStream fos = new FileOutputStream(dll);
            byte[] array = new byte[1024];
            for (int i = is.read(array);
                    i != -1;
                    i = is.read(array))
            {
                fos.write(array, 0, i);
            }
            fos.close();
            is.close();
            System.load(dll.getAbsolutePath());
        }
        catch (Throwable e)
        {
        }
    }
}

我的主类也遇到了一些问题(NetBeans不希望我的JAppletForm成为我项目的主要类,但是我可能会重新创建项目,因为我尝试了数百个补丁破坏了构建。我的主要类只是使用ConfigEngine的GetMap加载HashMap并在控制台中显示它是否为本地,或者如果它与webstart一起运行在JAppletForm中。

这是一个非常大的问题,所以如果我被问及您将需要的所有信息,我会更新我的问题。

0 个答案:

没有答案