I have an application which uses one native library "libSample.so" which is depend upon another .so file.I am trying to load that library using following code
File File1 = new File("libSample.so");
static
{
try {
System.load(File1.getAbsolutePath());
} catch (UnsatisfiedLinkError e) {
System.out.println("Link Error");
}
}
Before loading library I have tried setting up LD_LIBRARY_PATH where the library is located using command line.
export LD_LIBRARY_PATH=/home/usb:${LD_LIBRARY_PATH}
But still the library not get load.
What should I do now?
Please help.
static {
System.loadLibrary("libSample.so");
}
I assumed that you have your jars in /libs directory and .so file in /libs/armeabi directory so the system finds them. You do not have to add .so files in your eclipse build path.
Related
I'm working on a java project that uses both Jogl and JInput, and I working in IntelliJ. I'm having some issues with the application not being able to find various DLLs. I know the recommended way is to extract the DLLs to a folder and then point the java.library.path at that folder. Is there a way to include those libraries in project configuration somehow? I'm pulling those files from jCenter, and I'd rather just point the jar files and let JNA do its work.
I use this method below to set the path dynamically just before the DLL is needed by some object. It is called as follows:
setDllLibraryPath("C:/yourPathToDLLs")
Method to set library path
public static void setDllLibraryPath(String resourceStr) {
try {
System.setProperty("java.library.path", resourceStr);
Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);//next time path is accessed, the new path will be imported
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
So I am trying to integrate CLE in Android and for this reason I have to add a few .so files. I am following a tutorial from here
Everything works fine till integrating the Star libraries, however when I run the program I get the following exception:
Caused by: java.lang.UnsatisfiedLinkError: dlopen failed: library "/data/data/com.srplab.starcore/lib/libstar_java.so" not found
Now to include these .so files I have created a folder jniLibs and placed .so files for 3 architectures:
I ahve added the following lines of code to my build.gradle:
sourceSets
{
main
{
jniLibs.srcDirs = ['src/main/jnilibs']
}
}
However again when I run the below piece of code:
try {
//System.loadLibrary("mysharedlibrary");
System.load("/data/data/"+getPackageName()+"/libstar_java.so");
} catch (UnsatisfiedLinkError use) {
Log.e("JNI", "WARNING: Could not load libmysharedlibrary.so");
use.printStackTrace();
}
I am getting an exception, means the .so files did not load.
What is the proper way of doing this?
Your "armeabiv7a" folder must be renamed to "armeabi-v7a".
Also there is typo in :
jniLibs.srcDirs = ['src/main/jnilibs']
Your folder is with big L:
jniLibs.srcDirs = ['src/main/jniLibs']
By the way, "src/main/jniLibs" is used by default for native libraries and you do not have to specify it in your gradle scripts.
There is not full block of code above, but do you load your library from static initializer? like:
static
{
System.loadLibrary("star_java");
}
And look at your apk to be sure that your lib were successfully gathered. You can see it at app/build/outputs/apk/
I'm having this problem where none of the resources load when I run a JAR of the program. I've changed code and checked that it is indeed both the code that loads images and the code that loads sounds do not work. It works perfectly fine when I run it in eclipse. I checked with 7-Zip and the sounds and images files aren't in a res folder anymore. I tried changing the path files and the location of the resources in my program before I exported it, but that didn't work. I also have the res folder as a source folder in the build path. I'm exporting the JAR with eclipse, and then adding my libraries into it with JarSplicer. Here are the codes that load images and sound resources:
-Load Sound-
public static WaveData LoadSound(String path){
WaveData sound = null;
try {
sound = WaveData.create(new BufferedInputStream(new FileInputStream(path)));
} catch (Exception e) {
e.printStackTrace();
}
return sound;
}
-Load Images-
public static Texture LoadTexture(String path, String fileType){
Texture tex = null;
InputStream in = ResourceLoader.getResourceAsStream(path);
try{
tex = TextureLoader.getTexture(fileType,in);
}catch(IOException e){
e.printStackTrace();
}
return tex;
}
Here's my error in the command prompt:
Here are the files in the jar every time I exported it (regardless of whether the resources were in res or not):
I'm stumped here. If someone knows how to fix this, then please help me out.
I found the answer to my problem.
ResourseLoader.getResourceStreamAs() does not work inside of JAR files, so you need to do getClass().getResourceStreamAs(), or [ClassName].class.getResourceStreamAs(), if it's static.
Also, I had to change the location of the files from res/[resource file]/[resource] to /[resource file]/[resource] because when you export, it takes out all of the files in res. Also, you have to make sure that you have that / at the beginning of the path there in order to designate it to search in the source folder, or else it will search in the folder of the class that called getResourceStreamAs(). And also, new FileInputStream() doesn't work in JAR files, so you have to use [ClassName].class.getResourceStreamAs() instead. Oh, and on top of that, I had a few files that somehow got the extension part its name in all capitals for no reason. So that gave me some confusion. I basically just had to do a bunch of fiddling with code, but I got it working!
One more thing: make sure you add your resources folder to the sources tab in the build path, or else eclipse won't export it. And if you want your libraries to be exported in your JAR, then you'll have to add them manually by making a fat jar with JarSplicer.
Turns out that paths to resources are case sensitive and do not work in jar files if there is a single misstyped letter in the path.
I have a plugin, which is using some plain text and binary files when running.
This is because plugin is using some third-party code, which works as in conventional application, i.e. taking data from within application directory.
When I was running plugin from within Eclipse, these data was just laying inside project directory in some folders.
To access this data I was using code like
public static final String CorePropertiesPath = "conf/core.xml";
public static URL CorePropertiesURL;
//...
Bundle bundle = Platform.getBundle(ID);
CorePropertiesURL = bundle.getEntry(CorePropertiesPath);
try {
CorePropertiesURL = FileLocator.resolve(CorePropertiesURL);
} catch (IOException e) {
e.printStackTrace();
}
I.e. to access data from file in "core/core.xml" in my project's directory, I was first converting it with getEntry() method and then with resolve() method.
This was working.
But when started to create products, I found that my files like "core/core.xml" just absent in target directory. Probably they should reside in my bundle jar, but they are not there.
How to force them to come in prescribed place?
Check build.properties file (you can edit it on the 'Build' tab of a manifest editor). Add
Eclipse-BundleShape: dir
in your MANIFEST.MF if you want to generate a directory and not a jar for your bundle.
I've been trying to use Thrift in my android app to handle RPC's with a server. I add the thrift .jar file to my class path (Properties > Java Build Path > Libraries). I get no problems during compilation but during runtime I get a NoClassDefFoundError when using anything from the jar.
Do I have to do anything else to tell eclipse/android where libraries are located?
Cheers!
TSocket transport = null;
try {
transport = new org.apache.thrift.transport.TSocket(sock); <--- OFFENDING CODE
} catch (TTransportException e1) {e1.printStackTrace(); }
Please copy the jar file to project then add it to build path. I think you added from external path so it was not added in apk file.