JNA loading Library with dependencies - java

I need to create an original .so library (C code) that uses another .so library. After creating my .so library I wrote a short test program code on C that invokes functions of my lib. It works, but when I compile this program I use that command:
gcc my_test.c -lm -o my_test_program -lmy_lib -lother_lib -L
I need to use my library in Java code (JNA) but when I try to invoke functions from my lib I get this error:
/usr/lib64/libmy_lib.so.0: undefined symbol
I think that means it has lost dependence of other_lib.
What can I do to add dependence in my_lib? Or other solutions?

Related

Issue loading shared library in Java

I wish to make a java implementation based on a C++ library using the JNI. Unfortunately, when I wish to use the shared library in java it can not be found. The problem with this shared library is that it links another shared library. I have two different cases one works (but shouldn't be used) and the other one doesn't.
The Ana lib must always be static. The Clara lib must always be shared. It works when the Bob lib is static, but it should be shared and it should include Ana and Clara.
Case 1 (Works and can be loaded in the correct java.library.path).
sharedlibJava.so links: staticlibAna.a staticlibBob.a sharedlibClara.so
Case 2 (Doesn't work because it can't be loaded even though the java.library.path is correct).
sharedlibBob.so links: staticlibAna.a and sharedlibClara.so (the basic c++ implementation is tested and it works without problems)
sharedlibJava.so links: sharedlibBob.so (the java implementation should be available using this new shared library).
The problem is that when I try to load the library it doesn't even find it in the correct java.library.path even though it is located there.
I would really appreciate any feedback regarding this issue.
Put all your path locations on LD_LIBRARY_PATH. Alternatively, put them into -Djava.library.path
If you have compiled code properly, I don't think you should experience issues while accessing shared library.
If you compile one code as shared lib, and then refer to it by linking other code with shared lib, it should work just fine
cc -g -shared -fpic c/recipeNo023_AnotherFunction.c -o lib/libAnotherFunction.$(EXT)
cc -g -shared -fpic -I${JAVA_HOME}/include -I${JAVA_HOME}/include/$(ARCH) c/recipeNo023_HelloWorld.c -L./lib -lAnotherFunction -o lib/libHelloWorld.$(EXT)
Then, you should be able to call the code by passing lib location inside -Djava.library.path
java -Djava.library.path=:./lib -cp target recipeNo023.HelloWorld
library: :./lib
Hello world!
Hello from another function!
For the full sample code, take a look here:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo023

Unsatisfied link error when calling from NetBeans package

I have developed a linux shared library to communicate with pos terminals. The library has been developed using C language, and the routines are imported in to a java program using JNI interface. I have tested the library with the simple Java class it works perfectly.how ever when I try to develop a front end java application using netbeans it pops up Unsatisfied link error . so i recompiled the library with the package name embedded in ever function call with in the shared library and the issue was resolved. I found the package name causes the specific error. Is there a way to come out from this problem. because I cant ask the client to keep the package name static. it should be able to change as they need. I tried several compiler and linker directive but failed ..The current working library has been compiled using the following command
gcc -shared -o libecr.so -fpic ecr.c -lusb-1.0
This compiles with no errors in Fedora 14.
libecr.so is my shared library
ecr.c is the source code file
lusb is the libusb which is referenced by ecr code

How to use jnaerator in a sbt project

I work on a Scala project that uses c++ code, using sbt. Once compiled, this c++ code is imported into Scala through Java code that uses jna.
Now, currently the Java wrapper are manually written, and I like to automatize this. I've found jnaerator that can do that, but I don't know how I should use it in sbt.
I see two general approaches:
use command line, such as java -jar jnaerator ... but I don't know how to setup such command line task in sbt? Also, I would need to know the typical project structure to follow: where to output the jna generated code?
Use jnaerator maven plugin through sbt, if it is possible?
This might take some iteration until we get it do what you need.
For the first approach, here is how you can run custom system command on sbt (you essentially solve this using Scala code). Add the following to your build.sbt file:
lazy val runJnaerator= taskKey[Unit]("This task generates libraries from native code")
runJnaerator := {
import sys.process._
Seq("java" , "-jar", "jnaerator", "..." ).!
}
To execute:
>sbt runJnaerator
Now the question is where do you need these files files to go? Finally, how do you want to invoke everything?

No class located error in Matlab while calling java class

I have developed an Matlab code for stegnography which requires some preprocessing to be done by a Java file.
The java file aes1.java contains the function encrypt(String s) which needs to be called by matlab code:
javaaddpath('C:\Users\Aneesh\Desktop\BE_Project');
disp(char(javaMethod('hash', 'aes1', userText)));
I am getting the following error:
Error using javaMethod
No class aes1 can be located on the Java class path
Error in project_mod (line 11)
disp(char(javaMethod('hash','aes1',userText)));"
Versions I'm using:
Matlab 2013a
Java 1.6
Any help would be greatly appreciated!
Update:
I have a .jar file which contains all the classes i need. I've imported it and tried using it but I'm still getting the same error!
Please take a look at the documentation. javaaddpath adds one or more folders of java Archive (jar) files.
You need to compile your java file to a class-file and add it using javaclasspath

Can not integrate .so library from working project to another one

I have working project where I use JNI to call methods from C library.
My project's structure:
And code which load library:
static {
System.loadLibrary("RemoveBackground");
}
It works good. But until I try to integrate this functionality in other project. I copied jni and libs folders. Also all three classes without RemoveBackgroundActivity (test activity). And when I compiled this project I have an exeption:
1663-1663/com.example.Activities E/dalvikvm﹕ The lib may be ARM... trying to load it [/data/data/com.example.Activities/lib/libRemoveBackground.so] using houdini
1663-1663/com.example.Activities E/dalvikvm﹕ dvmHoudiniDlopen returns 0x9833cf40 with bool=1
Do you know how to solve the problem or some other way to do this?
I suppose that the RemoveBackground.so native library you try to use wasn't built for ARM architecture. I'm not sure about Linux, but on Mac OS X you can check the supported architectures of a native library using lipo command. For example:
lipo -info /usr/lib/RemoveBackground.so

Categories