8

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.

jesterjunk
  • 2,342
  • 22
  • 18
KaiserJohaan
  • 9,028
  • 20
  • 112
  • 199

3 Answers3

6

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;
    }
});
WhiteFang34
  • 70,765
  • 18
  • 106
  • 111
  • 1
    If my applet is signed, do I still need to call AccessController? – KaiserJohaan Apr 04 '11 at 10:56
  • Yeah, see http://stackoverflow.com/questions/1713403/calling-a-dll-from-an-applet-via-jni/1730904#1730904 also. – WhiteFang34 Apr 04 '11 at 15:50
  • I see. I forgot to mention, my DLL that I load, will in turn load more DLL's (all are located in the jar). Do I need to set an environment variable for that, or is it enough that my first DLL (loaded from java) is in the same directory (temp) as the other extracted dlls which it will load? – KaiserJohaan Apr 05 '11 at 07:09
  • Ah, that's a good question. I believe the DLL will only search the `PATH` environment variable and you won't be able to override that from inside of Java, so that probably won't work either way. I can think of two potential options though: 1) extract all of the DLL's to the temp directory and `System.load(..)` them one by one from lowest dependency first or 2) extract all of the DLL's to a known `PATH` directory and `System.loadLibrary(..)` the main one. – WhiteFang34 Apr 05 '11 at 09:25
0

If the user has a Next Generation plug-in2 JRE, the. applet can be embedded using Java Web Start. JWS makes it easy to add natives to the run-time class-path of an application or applet.

If the user does not have the plug-in 2 JRE, you can still launch the applet (free-floating) using JWS.

If deployed using JWS, setting environment variables should be unnecessary.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Have a look at JNA, This might solve your problem

http://jna.java.net/

Manish Singh
  • 3,463
  • 22
  • 21