I have a Xamarin project and an android bindings library that has a jar file attached. How can I call a function from the jar library. For example, so that when a button is pressed, a function from the library is called.
Sample project:
For this, you can check document Binding a .JAR.
The Android community offers many Java libraries that you may want to use in your app. These Java libraries are often packaged in .JAR (Java Archive) format, but you can package a .JAR it in a Java Bindings Library so that its functionality is available to Xamarin.Android apps. The purpose of the Java Bindings library is to make the APIs in the .JAR file available to C# code through automatically-generated code wrappers.
Xamarin tooling can generate a Bindings Library from one or more input .JAR files. The Bindings Library (.DLL assembly) contains the following:
The contents of the original .JAR file(s).
Managed Callable Wrappers (MCW), which are C# types that wrap
corresponding Java types within the .JAR file(s).
The generated MCW code uses JNI (Java Native Interface) to forward your API calls to the underlying .JAR file. You can create bindings libraries for any .JAR file that was originally targeted to be used with Android (note that Xamarin tooling does not currently support the binding of non-Android Java libraries). You can also elect to build the Bindings Library without including the contents of the .JAR file so that the DLL has a dependency on the .JAR at runtime.
For more information, you can check: Binding a Java Library .
Related
This applet was run through javaws. The jnlp file allows me to download the base jar, as well as some native libraries, compacted in separate jars for each OS.
With java support ending for browsers, I wanted to allow the applet to run as a normal app on the client, but with a web server on top of it.
So far, I can call methods on the main/base jar. However, this jar calls System.loadLibrary to load such native libraries. However, I always get an UnsatisfiedLinkError.
The native library jars are dependencies of the project, and I've even tried extracting the dylib and dll files from the jars. I've set the java.library.path flag to the path where they're at.
Nonetheless, I still get java.lang.UnsatisfiedLinkError: no library in java.library.path: /Users/me/java/apps.
The web server I'm using is NanoHTTPD.
Can dependencies access local files? Do I need to give java permission to access the directory? Do I need to name the jars containing these native libraries accordingly?
I'm on MacOS (the lib files follow the naming convention "lib{{LIBRARY_NAME}}.dylib").
These third-party jars I’m using are signed
(base Java jar with Java classes and jars containing OS specific native libraries).
I have a project with 3 modules - a library and 2 apps that using this library. Now I want to add native-lib (one c file) into the library module, and use it in one of the library classes. I'm new to NDK so I followed the Google guide and created new project with NDK support and it works fine.
Then I copy the cpp directory, CMakeList, and kept the correct structure of the project.
Now, what happening is that the project compiles successfully, but crashes when I trying to call the native func from java class.
Exception: Method threw 'java.lang.UnsatisfiedLinkError' exception.
I had this exception with a subsequent ClassNotFoundException when I wrote a Java library that referenced other native libraries. I didn't use NDK like you did, but maybe these steps (that solved it for me) can provide you some new ideas:
I imported the native library (for me it was OpenCV for Java) in AndroidStudio as a library.
I put the native source files in the jniLibs folder of the (imported) library.
Then I exported the library as AAR file. (Make Module -> take out the AAR from /build/outputs/aar/)
I used the AAR file in my own library. That way the classes of the referenced native library could be used in my own exported library.
I want to build Java application which uses JNI and code in C. Moreover I want make my app cross-platform by compiling native code and packaging native platform-dependent libraries within JAR file. I was following this guide
but it is not working. I have no examples with maven-native-plugin nor NAR plugins. How is it possible to make java native methods cross-platform?
I would suggest to check the nar-maven-plugin for example.
I have generated a DLL froma JAR file using IKVM doing:
ikvmc -target:library mylib.jar
I would like to use the DLL in a Visual C++ project. What are the steps required to call dll functions from my own code? How do I know which methods are exported by the DLL and how do I call them without a header file?
The IKVM.NET is written for the .NET framework 2.0. The usage inside a Visual Studio C++ project can be difficult. But it should be possible to access it via COM interface. Take a look at this answer.
I am just starting to learn JNI. I have been following a simple example, and I have created a Java app that calls a Hello World method in a native library. I'd like to target Win32 and Linux x86.
My library resides in a DLL, and I can call it just fine using LoadLibrary when the DLL is added to the root of my Eclipse project.
However, I can't figure out how to get Eclipse to export a runnable JAR that includes the DLL and the .SO file for Linux.
So my question is basically; how would you go about creating a project in Eclipse and include several versions of the same native library?
Thank you,
Martin
For runnable JARs, what you need to do is extract to the temporary directory (maybe in a static { } block) and then load the DLL from that directory (using System.loadLibrary() in the same block). To do this, you need to subclass ClassLoader and override the findLibrary() method to allow libraries to be found in that directory. You can do whatever logic you need to here to load the particular platform libraries. To be honest, the naming for the libraries on the different platforms should be similar anyway -- I believe that you omit the 'lib' part when loading, and the extension. That's the gist of it. Probably easier to use One-JAR as the other poster mentioned :)
You might want to check out the One-JAR project. It lets you package your application and its dependencies (including native libraries) to a single jar file.