Calling C++ functions from Java - 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.

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?

Access external native functions using JNI

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!

Calling methods C#.NET from JAVA

I have a dll created out of a C#.NET project and I would like to call the methods from a java program. I was wondering as to what sort of opportunities exist to accomplish this and then I came across JNA and JNI. Which one should I use for my purpose? Any suggestions? All I need is to call the methods in a class written using C#.NET and process the results from my Java program.
It would depend on your application, But you could place the C# DLL within a service e.g. WCF and expose the functionality to the Java code that way. Using wsimport to generate your client code within Java.
It saves the need to use cross over tools like JNI...

What are native libraries and native methods interface in JVM?

I came across this figure while studying JVM. I understood all the components except "Native method interface" and "native method libraries". What are those exactly?
Native method interface: Native method interface is an interface that connects native method libraries (implemented in C, C++ etc.) with JVM for executing native methods.
Native method library: Implementation in native code.
Please refer this link for more information.
Native Method Interface (JNI) is part of JDK that connects Java code with native applications and libraries that are written in other programming languages, such as C, C++, and assembly.
Why use JNI?
Use features that are platform-dependent and are not supported in the Java class library.
Increase performance by implementing a time-critical code in a lower-level language.
Access libraries that already written in another programming language.
Native Method Libraries are libraries that are written in other programming languages, such as C, C++, and assembly.
These libraries can be loaded through JNI.
So, the picture you posted is saying that JNI allows access to Native Method Libraries.
Java Native Interface:
The above diagram you could come across when you are studying about java virtual machine functionality. There is nothing like Native library when you are installing and working with java at first time. Those are all added when we are developing our own library , but it should be in other languages.
When you are developing functionality in other languages Java Virtual machine will include those libraries at the execution level(Third level) of the java application.
To add to this, there are also native libraries in jvm $JAVA_HOME/jre/lib/amd64/ and these are core libraries which are loaded by reflection(null/boot classloader) which is why they are available at run-time while compiling and we're able to use native methods of Object class like getClass(). So, not only for custom development using JNI but also, some of the core functionalities of java are written into native.

remote api for java

Is there a java api similar to RAPI? I want to be able to access files on the windows mobile device using a java desktop program.
Thanks.
You could use RAPI itself, and access it from Java using JNI or a wrapper like Swig
We were also looking for a similar API in Java but unfortunately none is available. I wrote my own RAPI wrapper using JNI and used that in my program.
The main problem with JNI is that any un-handled exceptions/faults cause the calling Java program to shutdown as well. Do keep that in mind when writing your wrapper. There are different approaches to safe guard these, the simplest and common approach is to write a standalone program written in .Net/C++ that communicates with the RAPI and your Java program communicates with that program using pipes/files etc. this way you also don't have to write a JNI wrapper :-)

Categories

Resources