Resolving UnsatisfiedLinkError in JNI Projects: Missing Dependent Libraries
In a JNI project, an UnsatisfiedLinkError with the message "Can't find dependent libraries" can arise when a custom library, such as mylib.dll, relies on a third-party library (e.g., libsndfile-1.dll) that is not available in the system search path. To resolve this issue, there are several key steps to follow:
-
Ensure Correct DLL Placement: Place both the custom DLL (mylib.dll) and the required dependency (libsndfile-1.dll) in the same directory as the Java application's JAR file.
-
Verify DLL Function Naming: Check that the function names in the custom DLL match exactly with those defined in the JNI header file. If necessary, adjust the linker flags in the build process to ensure correct method naming.
-
Consider Windows-Specific Behavior: On Windows, if the java.library.path system property is not explicitly set, the DLL must reside in the current working directory or a directory included in the Windows PATH environment variable.
Additional Troubleshooting Tips:
-
Verify Dependency Walker Warnings: Even if dependency walker shows unresolved imports for MPR.DLL and SHLWAPI.DLL, these warnings can often be ignored, as mentioned in the DW FAQ.
-
Check Shared Library Search Path: Use the command "java -XshowSettings:properties -version" (available in Java 8 and later) to display the shared library search path and verify that the required DLLs are accessible.
-
Eliminate Classpath Issues: The classpath and the shared library search path are separate and unrelated. Ensure that all required JAR files are correctly included in the classpath.
By carefully following these steps, developers can resolve UnsatisfiedLinkErrors related to missing dependent libraries in JNI projects, allowing their applications to run smoothly.
The above is the detailed content of Why Does My JNI Project Throw 'UnsatisfiedLinkError: Can't Find Dependent Libraries'?. For more information, please follow other related articles on the PHP Chinese website!