开发者

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.


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


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.


Have a look at JNA, This might solve your problem

http://jna.java.net/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜