Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - write dll files from inside a jar to the hard drive?

I have a signed applet and I want to write out dll files which are contained in the jar from which I launch my applet.

I am doing this because I then want to do a System.load on the dll's, as apparently you can't load DLL's from inside a jar in an applet.

The second issue is if you can add to the environment variables in an applet - for example I want to extract my DLL's to a location the hard drive and add the environment variable so System.load can find it.

like image 555
KaiserJohaan Avatar asked Mar 31 '11 07:03

KaiserJohaan


1 Answers

You should be able to accomplish this by:

  1. Extracting the .dll from the applet jar into the system temporary directory.
  2. Calling System.load(..) on the extracted file with AccessController.

This approach would avoid the need to set an environment variable. Here's some example code:

AccessController.doPrivileged(new PrivilegedAction<Void>() {
    public Void run() {
        String dllName = "my.dll";
        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
        File tmpFile = new File(tmpDir, dllName);

        try {
            InputStream in = getClass().getResourceAsStream(dllName);
            OutputStream out = new FileOutputStream(tmpFile);

            byte[] buf = new byte[8192];
            int len;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }

            in.close();
            out.close();

            System.load(tmpFile.getAbsolutePath());

        } catch (Exception e) {
            // deal with exception
        }

        return null;
    }
});
like image 69
WhiteFang34 Avatar answered Nov 14 '22 22:11

WhiteFang34