Cannot delete library loaded with JNA after disposing

I am trying to delete a DLL which has been loaded into JNA and later disposed. I have tried all the solutions described in the answer to this question, but they are not working: How to dispose library loaded with JNA

Here is code I've tried without a time delay:

import java.io.File;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

class Filter {

    private static ExtDLLTool DLLUtil;
    final private static String dllPath = "./ExternalDownloader_64.dll"; 

    static {
        DLLUtil = (ExtDLLTool) Native.loadLibrary(dllPath, ExtDLLTool.class);
    }

    public static void main(String[] args) {

        if (DLLUtil != null) {
            DLLUtil = null;
            NativeLibrary lib = NativeLibrary.getInstance(dllPath);
            lib.dispose();
        }

        File dllFile = new File(dllPath);
        if(dllFile.exists()){
            boolean isDeleted = dllFile.delete();
            if(!isDeleted){
                System.out.println("Unable to delete dll file, since it hold by jvm");
            }
        }

    }

    private interface ExtDLLTool extends Library {
        String validateNomination(String dloadProps);
    }
}

I added a time delay to give the native code time to release the handle:

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;

class Filter {

    private static ExtDLLTool DLLUtil;
    final private static String dllPath = "./ExternalDownloader_64.dll";

    static {
        DLLUtil = (ExtDLLTool) Native.loadLibrary(dllPath, ExtDLLTool.class);
    }

    public static void main(String[] args) throws Exception{

        if (DLLUtil != null) {
            DLLUtil = null;
            NativeLibrary lib = NativeLibrary.getInstance(dllPath);
            lib.dispose();
            Thread.sleep(3000);
        }

        File dllFile = new File(dllPath);
        if(dllFile.exists()){
            Files.delete(Paths.get(dllPath));
            // boolean isDeleted = dllFile.delete();
            if(dllFile.exists()){
                System.out.println("Unable to delete dll file, since it hold by jvm");
            }
        }
    }

    private interface ExtDLLTool extends Library {
        String validateNomination(String dloadProps);
    }
}

This code results in an exception implying the JVM has not released the file.

Exception in thread "main" java.nio.file.AccessDeniedException: .\ExternalDownloader_64.dll at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation