how to call c functions from client dynamic library in Java - 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?

Related

How to call a function in a c++ object file from java?

I have existing cpp files from a project which I would like to use it for my application which is in Java. I cannot change the cpp files. How can I call the functions from Java?
I'm working in Windows 10 using JavaFX for my application. I've seen some articles about JNI but none seem to solve my issue.
If JNI or swig is not desired or seems too low level,
A really blunt approach is to wrap the .cpp in c/c++ program and built an .exe that dumps to stdout/file. Then execute that in java via an external shell command.
Another good alternative is
Apache thrift
This basicly handles everything and goes everywhere so to speak (works by auto-generating code to target languages) and it is one I usually recommend in RPC situations. However there could be more setup cost involved (in the end, depends on your actual needs) - also since you need to host the .cpp in a service, in your case, locally.
If you package your library inside a shared object or a dll, you can also use JNA: https://github.com/java-native-access/jna or https://github.com/java-native-access/jna/blob/master/www/GettingStarted.md
For example, you already have mapping to Windows API.
Another example is a mapping of mediainfo in Java: https://github.com/andersonkyle/mediainfo-java-api/blob/master/src/main/java/org/apothem/mediainfo/api/MediaInfo.java
Note that, as far as I understand it, this is based on JNI: it simplify the process since you mostly have to only declare interface on Java side and call appropriate method.

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...

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.

Categories

Resources