Applet不加载本机库

时间:2013-10-11 12:15:35

标签: java applet

我正在开发一个Applet。小程序是有罪的,一些代码完美无缺。 我在本地保存了一个从我的远程站点读取的库:

AccessController.doPrivileged(new PrivilegedAction() {
    public Object run() {
        try{
            URL url = new URL(getCodeBase(), "./dyn_libs/libusb4java.so");
            byte[] buffer = new byte[1024];
            InputStream is = url.openStream();
            String tmpDir = System.getProperty("java.io.tmpdir");
            String fileSeparator = System.getProperty("file.separator");
            String userName = System.getProperty("user.name");

            FileOutputStream os = new FileOutputStream(tmpDir+fileSeparator+"libusb4java.so");
            int nRead=0;
            int total=0;

            while((nRead = is.read(buffer))!= -1){   
                os.write(buffer);
                total+=nRead;
            } 
            is.close();
            os.close();

        }catch (Exception e){
            PrintWriter writer = null;
            try {
                writer = new PrintWriter(new FileWriter("/tmp/salida.txt", true));
                writer.println("error en el catch"+e.getMessage());
                writer.close();
                return "error en el access controller : "+e.getMessage();
            } catch (Exception ex) {
                Logger.getLogger(fpc_01.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                writer.close();
            }
        }

        return null;
    }
});

但是当我想加载本机库时没有任何反应,我的意思是,没有错误,applet只是停止运行。我用:

加载本机库
try {
    String resultado= AccessController.doPrivileged(new PrivilegedAction<String>() {
        @Override
        public String run() {
            PrintWriter writer = null;
            try {
                writer = new PrintWriter(new FileWriter("/tmp/salida.txt", true));
                // privileged code goes here, for example:
                System.load("/tmp/libusb4java.so");
                writer.println("exito");
                writer.close();
                return "exito"; // nothing to return
            }
            catch (Exception e)
            {
                writer.println("error en el catch"+e.getMessage());
                writer.close();
                return "no puede cargar biblioteca";
            }
        }
    });              
}
catch (Exception e)
{
    PrintWriter writer;
    writer = new PrintWriter(new FileWriter("/tmp/salida.txt", true));
    writer.println("error en el catch"+e.getMessage());
    writer.close();
    return "problema al cargar";
}

欢迎任何线索。 TIA 亚历

1 个答案:

答案 0 :(得分:0)

如果您在applet中使用jnlp文件,那么您也可以尝试使用nativelib tag。因此,必须将本机lib打包到一个JAR文件中,该文件包含其根目录中的本机库。 jnlp文件的一部分看起来像这样:

<nativelib href="http://yourremotesite.com/dyn_libs/libusb4java.jar"/>