Loading 2 .so native libs in linux from Java via JNI - java

I'm having some problems loading 2 libs where one depends on another in Linux. Let's say I have 2 libs, libA.so and libB.so, libB.so depends on libA.so (calling functions from it).
I need to load libB.so from Java via JNI and call some native methods from it.
SO what I'm trying to do is:
static {
System.loadLibrary(A);
System.loadLibrary(B);
}
(Both libraries reside in java.library.path).
Under Win32, it works fine - B.dll sees that A.dll is already loaded, and doesn't try to load it itself (using PATH lookup).
While on Linux, it doesn't work. Additional logging shows, that System.loadLibrary(A); executes correctly, and libA.so is getting loaded fine, then, when we I try to load B, it looks for library libA.so in the LD_LIBRARY_PATH, and it fails (both libs are in java.library.path, but NOT in LD_LIBRARY_PATH).
Does someone why does it happen? Is it related to the way Linux runtime linking works?
I see a lot of way to work it around, but first want to understand the bottom line of that.
Thanks,
Mikhail

Related

Java JNI, multiple versions of a dll: How to specify which dll is being used for native calls (matlab jvm)

I am facing a special situation. I am trying to run a java application within matlab. My application uses an external dll which depends on another dll (xerces-c_3_2.dll). My problem is that matlab also contains a xerces-c_3_2.dll in its root folder.
Unfortunately, these two dll files are different! It is not possible to change the library path of the jvm within matlab in a way, that the 'matlab'-version of the dll is not shadowing my dll version (it's automatically loaded on matlab startup). Due to this, my application is always throwing exceptions that a procedure could not be found because its using the wrong version.
Since matlab won't start with my version of the dll, my idea is now to rename the dll to 'xerces-c_3_2_myVersion.dll' and load it redundantly.
How can I tell the jvm for a specific jni call which native interface should be used?
In my jni interface the known
public final static native lines are defined, but I never faced the question how to specify the dll in case of redundant native functions?
Any ideas? Thank you!
Sven
I have resolved my problem:
I renamed my version of the dll file from 'xerces-c_3_2.dll' to 'xerces-c_3_s.dll'
I modified my compiled personal dll in a hex editor and changed the dependency acc. to the naming in (1)
I loaded the modified dll with the changed dependency using java within matlab. Now it's working without any problems!
So technically it was more a windows library thing rather than a java question.

Cross-platform Java with native libraries

I am trying to implement a wrapper for native libraries that will be consumed via JNI in a Java application. I want to make installation of the program be as simple as "Unzip this file and run".
I've worked through most of the wrapper so far, getting it to unzip the native libraries from inside the platform-specific JAR file and setting the -Djava.native.lib variable at runtime. I've hit one last roadblock due to unsatisfied link errors.
On Windows, the DLLs that I'm calling link against msvcr100.dll. While the DLLs that I'm calling into are getting picked up by java.native.lib, msvcr100.dll isn't. I don't want to have to require the user to install the C++ runtime before using our app, so we are packaging the DLL file right along side the other DLLs.
As far as I understand, to get things to work we need to put the folder that contains msvcr100.dll on the PATH. I tried using reflection to change the environment inside the application to add the unpack directory to the PATH environmental variable. After this was unsuccessful, I recalled that I was only modifying a copy of the environment.
Is there some way that I can fix the unsatisfied link error without requiring any additional system setup (installing the runtime, hand modification of PATH, etc)? I would think it is possible since I've encountered other pieces of software (SWT, JOGL) that package native libraries that don't exhibit this issue.

Loading multiple dependent libraries via JNI and DllMain

I have a JNI library that depends on a third party library.
I can load and use the library by first doing a System.Load on the third party library then doing System.Load on my library. This works.
I recently introduced a DllMain (does not do anything) in my library and now when the library is loaded I get java.lang.UnsatisfiedLinkError: fullpath\name.dll: Can't find dependent libraries
If I attach a native debugger to the process and set a breakpoint in the DllMain function the breakpoint is entered and the Library is loaded correctly (no UnsatisfiedLinkError )
Does anyone have any idea or pointers as to what might be going on?
I suppose that in you didn't link dlls that your dll are using to you java runtime. It must be in your working directory or use -Djava.library.path=fullpath.

Calling dlopen from a shared library loaded via JNI makes previously loaded symbols invisible

I have a Java application which interacts with native code using JNI. The native code compiles a file at runtime and attempts to load this file using a dlopen call. This call fails and I get a warning stating
Could not load library (x):(x) undefined symbol: y
However when I have a native application starting a JVM via JNI run the same code, this error does not occur and it runs as expected. I am suspecting Java is doing something fancy which causes the already loaded libraries to be invisible for the library that is loaded with dlopen.
This I did to diagnose the problem:
Confirmed in which shared library the symbol that was supposedly undefined is located using objdump
Confirmed this library is loaded by using gdb (via eclipse - the library was listed in the modules pane)
Printed LD_LIBRARY_PATH just before the dlopen and confirmed Java passes it through (It did add Java's lib dirs but the original dirs were still there)
I have been trying to solve this for a while now, but I can't figure out what is going on. Especially because it does work when the JVM is loaded from a native application.
Thank you in advance!
I finally found the answer. The solution was to recompile the shared library that contained the symbol that was not found with the -Wl,--export-dynamic linker flag.
Interestingly I did not program that shared library myself and I would expect the default compilation to add the flag since it was needed for software using it to function properly.
Either way it is a open source project so I could compile it with the proper flags set.

UnsatisfiedLinkError: The specified procedure could not be found

I'm writing some JNI code in C++ to be called from an applet on Windows XP. I've been able to successfully run the applet and have the JNI library loaded and called, even going so far as having it call functions in other DLLs. I got this working by setting up the PATH system environment variable to include the directory all of my DLLs are in.
So, the problem, is that I add another call that uses a new external DLL, and suddenly when loading the library, an UnsatisfiedLinkError is thrown. The message is: 'The specified procedure could not be found'. This doesn't seem to be a problem with a missing dependent DLL, because I can remove a dependent DLL and get a different message about dependent DLL missing. From what I've been able to find online, it appears that this message means that a native Java function implementation is missing from the DLL, but it's odd that it works fine without this extra bit of code.
Does anyone know what might be causing this? What kinds of things can give a 'The specified procedure could not be found' messages for an UnsatisifedLinkError?
I figured out the problem. This was a doozy. The message "The specified procedure could not be found" for UnsatisfiedLinkError indicates that a function in the root dll or in a dependent dll could not be found. The most likely cause of this in a JNI situation is that the native JNI function is not exported correctly. But this can apparently happen if a dependent DLL is loaded and that DLL is missing a function required by its parent.
By way of example, we have a library named input.dll. The DLL search order is to always look in the application directory first and the PATH directories last. In the past, we always ran executables from the same directory as input.dll. However, there is another input.dll in the windows system directory (which is in the middle of the DLL search order). So when running this from a java applet, if I include the code described above in the applet, which causes input.dll to be loaded, it loads the input.dll from the system directory. Because our code is expecting certain functions in input.dll which aren't there (because it's a different DLL) the load fails with an error message about missing procedures. Not because the JNI functions are exported wrong, but because the wrong dependent DLL was loaded and it didn't have the expected functions in it.
There is a chance that the DLL was built using C++(as opposed to C). unless you took care to do an extern on the procedure,this is one possible reason.
Try exporting all the functions from the DLL. If the list includes your function, then you're good.
Usually, when linking to other libraries, you need to link to the relevant .lib file. It sounds like you aren't referencing all the lib files you need. Check what isn't linking and make sure you add it's lib to the list for the linker.
Did you create the new external DLL using the standard JNI procedure? I.e., using javah and so forth? If so, then I am not sure what is wrong.
If not, then the procedure you're trying to call hasn't been exported (as mentioned by anjanb). I am aware of two way of exporting functions: a separate export list and marking specific functions with __declspec(dllexport).
Can't access variable in C++ DLL from a C app has a little more information the topic of DLLs.
Compile your c++ code in debug mode. Then insert the DebugBreak(); statement where you would like to start debugging. Run the java code. When the DebugBreak() statement is encountered you will get a popup with a Debug button on it. Click on it. Dev Studio will open with your program in machine code. Step over with the debugger twice and you should be able to step over your source code.
If you have done all programming issue at JNI manuals and examples but still you are getting same missing procedure error, problem can be at your path variable probably. Do below steps and run again:
Be sure about you set JAVA_HOME variable to your JDK folder(not JRE because JRE doesnt contain jni header)
Example:
At environment variable settings panel define var:JAVA_HOME val:C:\Program Files\Java\jdk1.7.0_11
add %JAVA_HOME%\bin to your path variable
After doing those steps, your application can find jni procedure name and links to JNI.dll in right way. So, i hope you dont get this missing procedure error again.

Categories

Resources