0

I'm trying to load a dll file from a jar to work with the applet with this fix:

Extract and load DLL from JAR

I create the file, set it writable and executable, but when I check it with canWrite()/canExecute() it returns false. The applet is signed.

The code:

private static void loadLib() {
URL res = SystemActivityNotifications.class.getResource("/sys-native
    /sysactivitynotifications.dll");
InputStream in = res.openStream();
File dll = new File(path + "sysactivitynotifications.dll");
dll.setExecutable(true);
dll.setWritable(true);
logger.info(dll.canWrite() + " " + dll.canExecute());
FileOutputStream fos = new FileOutputStream(dll);
byte[] array = new byte[1024];
try {
    for(int i = in.read(array); i != 1; i=in.read(array)) {
        fos.write(array, 0, i);
    }
} catch (IOException e) { logger.info("Cannot write to file: " + e.getMessage()); }
fos.close();
in.close();
System.load(dll.getAbsolutePath());
}

The file is created properly, but it throws an exception while trying to write to it.

edit: it writes to the file the second time I run the applet, but if I delete the file and run again the first iteration doesn't work.

Forgot to mention: all the code above is from a catch block after System.load.library(dll); throws the exception.

try
    {
        System.loadLibrary("sysactivitynotifications");

        ptr = allocAndInit();
        if (ptr == -1)
            ptr = 0;
    }
    catch (Throwable t)
    {
        if (t instanceof ThreadDeath)
            throw (ThreadDeath) t;
        else {
           loadLib();
        }
    }

Edit: it throws me this error:

java.lang.UnsatisfiedLinkError: C:\: The process cannot access the file because it is being used by another process
Community
  • 1
  • 1
Dima O
  • 73
  • 2
  • 9

1 Answers1

1

..trying to load a dll file from a jar to work with the applet..

For deploying Java applets, the best option is usually to launch the applet using Java Web Start. JWS works on Windows, OS X & *nix.

Java Web Start (JWS) is the Oracle Corporation technology used to launch rich client (Swing, Java-FX, AWT, SWT..) desktop applications directly from a network or internet link. It offers 'one click' installation for platforms that support Java.

JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or locale, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..

Place the natives in the root of a (signed) Jar and put a reference to that Jar in the launch file and they will be placed on the class-path of the app.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Yes, thanks, but we are not using JWS, and the dll downloads & executes correctly but only in the second run. – Dima O Oct 22 '13 at 12:30
  • @DimaO were you able to figure out what the issue was? – amadib Apr 09 '14 at 14:49
  • Yes, but I don't remember exactly, I changed all the downloading code, and the DLLs are changed as well...if you have the same problem you could check what process locking the DLL, maybe download it to a different folder that's in java path – Dima O Apr 09 '14 at 15:17