Write DLL files from inside a jar to a hard drive?

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

I do this because then I want to do System.load on the dll, since, apparently, you cannot load the DLL from inside the jar in the applet.

The second problem is if you can add environment variables to the applet - for example, I want to extract my DLL to a place on my hard drive and add an environment variable so that System.load can find it.

+8
java jar file-io dll jni
source share
3 answers

You must complete this:

  • Extract .dll from the applet to the system’s temporary directory.
  • Call System.load(..) in the extracted file using the AccessController .

This approach avoids the need to set an environment variable. Here is a sample 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; } }); 
+6
source share

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 class path of an application or applet.

If the user does not have the 2 JRE plugin, you can still run the applet (free floating) using JWS.

When deploying using JWS, setting environment variables is optional.

0
source share

Look at JNA, this may solve your problem.

http://jna.java.net/

0
source share

All Articles