0

我正在开发一个小程序。小程序被烧毁,一些代码完美运行。我使用以下方法在本地保存从远程站点读取的库:

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;
    }
});

但是当我想加载本机库时,什么也没有发生,我的意思是,没有错误,小程序只是停止运行。我加载本机库:

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 亚历杭德罗

4

1 回答 1

0

如果您在 applet 中使用jnlp文件,那么您也可以尝试使用nativelib 标记。因此,本机库必须打包到一个 JAR 文件中,该 JAR 文件在其根目录中包含本机库。jnlp 文件的部分如下所示:

<nativelib href="http://yourremotesite.com/dyn_libs/libusb4java.jar"/>
于 2013-10-14T07:54:32.020 回答