News for hackers. Stuff that matters.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by zten on Wednesday May 08, 2024 @03:39PM (#40302053)
Yep, you're right, they do exactly that. Apologies for the confusion.

Decompiled class file:

    try {
        var4 = File.createTempFile("libzstd-jni-1.5.0-4", "." + libExtension(), var0);
        var4.deleteOnExit();
Re:How to Use the Foreign Function API in Java 22 to Call C ... by sedro on Wednesday May 08, 2024 @08:42PM (#40304154)
This wouldn't work on Windows, because you can't delete a DLL while it's in use
Re:How to Use the Foreign Function API in Java 22 to Call C ... by BenjiWiebe on Wednesday May 08, 2024 @09:57PM (#40304544)
Couldn't you: Extract DLL Load DLL Unload DLL Delete DLL ?

Though in the example given, I do see your point now. You'd have to make sure the DLL was unloaded before the delete-on-exit happened.

Re:How to Use the Foreign Function API in Java 22 to Call C ... by sedro on Wednesday May 08, 2024 @11:18PM (#40304991)
According to JNA it's not safe to unload the DLL:

https://github.com/java-native-access/jna/blob/40f0a1249b5ad...

  Do NOT force the class loader to unload the native library, since
  that introduces issues with cleaning up any extant JNA bits
  (e.g. Memory) which may still need use of the library before shutdown.
Following the blame back to 2011, they did unload DLLs before https://github.com/java-native-access/jna/commit/71de662675b...

  Remove any automatically unpacked native library.  Forcing the class
  loader to unload it first is only required on Windows, since the
  temporary native library is still "in use" and can't be deleted until
  the native library is removed from its class loader.  Any deferred
  execution we might install at this point would prevent the Native
  class and its class loader from being GC'd, so we instead force 
  the native library unload just a little bit prematurely.
Users reported occasional access violation errors during shutdown.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by tadfisher on Wednesday May 08, 2024 @10:58PM (#40304881)
You can install a shutdown hook to do cleanup like this.

    Runtime.getRuntime().addShutdownHook(...)
Re:How to Use the Foreign Function API in Java 22 to Call C ... by sedro on Wednesday May 08, 2024 @11:22PM (#40305010)
That's how java.io.File#deleteOnExit works under the hood. The DLL is still loaded at that point and can't be deleted.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by tadfisher on Thursday May 09, 2024 @03:51PM (#40312275)
Ah, looking through the docs [1]; you have to use your own ClassLoader (so it can be garbage-collected), and statically-link with a JNI library which is unloaded when the ClassLoader is garbage-collected.

1: https://docs.oracle.com/en/java/javase/22/docs/specs/jni/inv...

Re:How to Use the Foreign Function API in Java 22 to Call C ... by electrum on Thursday May 09, 2024 @12:30AM (#40305316)
You might be able to use FILE_FLAG_DELETE_ON_CLOSE, but this would likely require calling the Windows API functions directly.
Re:How to Use the Foreign Function API in Java 22 to Call C ... by zten on Thursday May 09, 2024 @12:27PM (#40309870)
Hmm, interesting. They do have DLLs in the JAR...