Access external native functions using JNI - java

I have a small doubt regarding native library access using NDK.
Is it possible to access native functions in the library using JNI? I have a library built completely on C++, is it possible to load that external library and write JNI to communicate with functions in the library?
If not is it possible to embed that JNI class within the library and communicate with it from Android activity?

Take a look at this sample code:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo025
There, you have a sample code that wraps C++ code inside JNI wrapper.
Unfortunately, all you can do (inside JNI) is to call native function.
Have fun with JNI!

Related

how to call c functions from client dynamic library in Java

I am new to native code programming in java. i have client dll where they have exposed apis and also given api document.I want to use them in java.
I have written some code to load native library using system.loadLibrary and declared few native methods and able to call them successfully.
But they have some callback methods as well and few data structures that needs to be passed. I have written simlar classes in Java seeing their api. But it is not working.
i am confused about JNI or JNa uses. do I need to use them and create any header files as I already native methods are implmented.
i suppose jni to be used only when we need to develop some part of code in other language like c c++.
Creating header files and then implementing and creating dll. But thats already with me.
Whats the way to easily write those typedef in java??
and is my understanding correct that i dont need jni or jna for this?

Calling a c++ callback from Java invoked from C++ application via JNI

I have a C++ application that needs to interface with a Java library. I'd like to receive notifications from the Java side so it needs to call back into the C++ app. I've found numerous examples on invoking Java functions from C++ (this is the easy part) and calling C/C++ functions from Java that are exported from a DLL.
Everything I've found so far is how to invoke Java callbacks from an external C++ function within a Java app. I need the opposite.
With straight C++ this, is of course, easy. I'd simply pass a function pointer to the class which would in turn call as a callback when needed. How can I do this when I'm instead invoking a Java function?
In case there's a better way altogether than what I'm asking for here is the overall application:
My C++ application needs to access an external server which will stream data back to my app. The access API is a Java based API. My current plan is to build a Java wrapper that will handle all of the API calls. This wrapper will be invoked from my C++ app using JNI. When data is received, it will process it as much as possible and then notify my C++ app with the adjusted data.
To sum up how can I call a C++ function callback from a Java class which was in turn invoked via JNI from a C++ application. There is no DLL to load for Java to use. Although I can make one if needed if everything can interface properly.
Function Path: C++ application -> Java class library -> C++ callback function
I found it. There is a function in the environment class called "RegisterNatives". With this I can register C++ callbacks for any Java class at runtime. I tried it and it works exactly as expected.

calling .DLL file through java

I have a DLL file mostly written in vb.net
It will take 2 parameters.
I am suppose to make use of this DLL in my java code and pass required 2 parameters.
How should I go about it?
Use JNI, Load your dll and call native functions
See
Examples
In Java there's basically two good options for this: (in order of recommendation)
If your DLL has C function headers (rather than C++ decorations), you should use JNA. It has a simple, declarative syntax and only requires writing some Java.
Write JNI bindings for your DLL (there'll be some Java and some C++ code involved).

Calling C++ functions from Java

I am developing a Java application in which I need to call some C++ functions (from Google Talk library libjingle) . The objective is to run it all on Google App Engine (which only supports Python or Java).
How can I do this?
You need to define native methods in your java code for whatever you want to be implemented in C++ and directly access your native code. Then you run javah on your code and it will generate the C header files for you and you'll need to provide C++ implementations.
The native methods you can call from your Java code like any other methods and still they'll have their implementation written in C++ and talking to whatever other native library directly.
You then need to set the java.library.path system property to include the shared C/C++ libraries that you require: the google library and your own JNI implementation library would be required in this case.
If the library has C bindings through a DLL/SO, I usually prefer writing wrappers in Java using Java Native Access (JNA) rather than writing the bindings in C/C++ using the Java Native Interface (JNI). The former is easier to manipulate as the JNI access to Java objects is a real pain in the neck. However, it's not as obvious to wrap C++ classes using that API.
You might also want to look into the Simplified Wrapper and Interface Generator (SWIG) for automating part of this process!
You can't run native code on App Engine - only JRE code. If there's no avoiding the native code, you'll need to run this part of your app on another system, and call it from your App Engine app - or use the built-in XMPP API, in this case.

Referencing a dll in Java

I need to reference a C++ dll from my Java project. The method that I need to expose is actually written in Visual Basic. Is there any way to access the Visual Basic code in C++, so that it can eventually be accessed in the Java project?
You can use OS native DLLs from your Java project using Java Native Interface (JNI):
jmac posted the original question on my behalf. I needed to find a way to call VB DLL function from a C++ DLL.
I've given up on the VB DLL and opted for a C# DLL. The following link provides a downloadable Visual Studio solution that provides a project called DLLExporter that exports C# functions thus making them available to C++. The VS solution was written in a version earlier than 2010 but the VS 2010 migrator had no problem importing it.
http://www.codeproject.com/KB/dotnet/DllExporter.aspx
It solved my problem.
For the record, I tried to wrap my VB DLL inside the C# DLL but it didn't work. So I just migrated my VB code to C#.
Assuming this is VB 6.0 and not VB.NET, you need to create an MFC DLL wrapper for your VB ActiveX DLL.
Here's more information on Exporting VB DLL Functions.
You have to use JNI (Java Native Interface) technology.
I highly recommend the Java JNI Tutorial.
as per Andriy Sholokh ,u need to use JNI to communicate with c or c++ from your java project. You have to use native method inside your java code. hope it will help you.

Categories

Resources