I have method/function to get the drive info using Visual C++ of Microsoft Visual Studio. The method only return the type of drive. But it is become difficult when I'm trying to get more information about the drive. This difficulty gets resolved by Java's existing package. Furthermore, few codes are also there which is easy to handle with C platform. So, I'm thinking, if there is any efficient process which can include a native C code with in Java so that the functionality of both the language can be used in meaningful way.
Can anyone help me in this regard?
Java Native Access (JNA) is a way you can access C libraries from Java code. Its advantage over Java Native Interface (JNI) is that the library glue code is written in Java, not C, making it more maintainable.
(I've used JNA a lot at work. If I get permission from work, I may be able to post the JNA stuff I've written, as examples for you to work with.)
Related
I am facing a critical issue trying to export my opencv project to android.
Since all of my code is in opencv (C++ version) under visual studio 2013, i started off looking into JNI. I ended up realizing that JNI works good when we export C stuff as name mangling is turned off. Does this affect my C++ code? I think it should as name mangling has to be there for C++ class functions.
Then I found out about JavaCPP. This seemed more promising but the extra overhead its Pointer logic seems strange to get my head around and extra build steps to create .so files.
And finally, read about Visualgdb. Though seems to be in trial, but with my liking for Visual studio, looks reasonable.
Can someone guide my which way I have to follow to translate/bind my code in android so that I can export my c++ classes? Does plain JNI have a real issue with C++ code and no problems with C? OR do I go for JavaCPP or visualgdb?
You valuable comments will be highly appreciated.
Thanks
AFAIK you can use JNI on your c++ code, I'm using it. But you should only use JNI if you want to write all your code in c++ and want java to use that exact c++ code in java side. And for that you should export all functions in your public API. And I must say, passing through custom objects such as Mat, Rect, etc is not a very easy problem, also debugging is not also straight forward. I suggest you to use OpenCV Java and port your code to Java.
Good luck!
I have a general question according an android app, I need to use some pcap functionality in my android app. Because java does not give the possibility in raw packet injections and low layer programming (as far as I know, pls correct me if I'm wrong) so I was looking for an alternative. So far I found the following:
ANDROID NDK
JNETPCAP
Any suggestions which one I should use or does anyone have other suggestions?
The JNI Solution
You need to wrap the calls and the logic you need out of libpcap in C or C++ and expose the underlying functions through JNI (Java Native Interface) so your application can call native code in Java.
The documentation on JNI is pretty complete on internet, a lot of tutorials exists on this subject such as this one.
If you want to easily wrap native code in JNI you can use Swig which allow you to automatically generate JNI code based on your C/C++ native headers.
The obtained JNI code should be compiled using the Android NDK as a dynamic library (.so). This library is to be placed in your application package under libs/. You can then invoke System.loadLibrary(path_to_you_dynamic_library) to load all the symbols contained in the library and use them in Java.
Using a third-party library
If you're afraid of getting headaches while figuring out how to use JNI, you can look at this library which does the hard work for you, and provides an API to manipulate raw sockets in Java.
http://www.savarese.com/software/rocksaw/
You need to wrap the calls and the logic you need out of libpcap in C or C++ and expose the underlying functions through JNI (Java Native Interface) so your application can call native code in Java.
Or you need to get a library that's already done that, such as, err, umm, jNetPcap.
One problem you may have with any attempt to do packet capture on Android - or any other OS using the Linux kernel - is that, by default, the underlying kernel mechanism used by libpcap (PF_PACKET sockets) requires root privileges. If there's a way to run your code as root, or to give it CAP_NET_RAW and possibly CAP_NET_ADMIN privileges, it might be possible to make it work.
I'm building a J2EE project, in which I would like to use an API which is only available in C.
I was thinking of using JNI to do so, but after a quick look at this tutorial, it looks that I in order to use JNI, I need to have the source code (.c files) to compile some kind of "JNI library".
In my case, the API only comports the .h with the signature of all the methods, and the already compiled .dll (or .so).
How could I do this?
Thank you!
JNA is a JNI-based library that allows calling normal C functions without needing a JNI-specific wrapper for each one.
Check out JNA. It allows you to use the .DLL directly. All you need to do is write a Java interface with the same functions you need from the .DLL.
Create a small C wrapper for the native library, compile this to a .dll/.so.
For each needed function in the existing C api, create one JNI-compliant C function which simply calls the real API.
JNI offers a pretty low level API for interfacing your Java code with native code. If you are OK with shelling out money, Jinvoke looks like a pretty good alternative which doesn't require you to write any C/C++ code. The plus here is that you get full paid support. If you don't require it, you can go with JNA. Anything but JNI IMO...
I am completely new to the NDK.
I have done a couple of the tutorials including the hello from jni one
and another one that calculates the sum of two numbers.
They involved using cygwin and the ndk to create the library so file
and I have a bit of a grasp on how to insert my own libraries into the
libraries layer of Android.
I have now been asked to access the native libraries on Android and
see what I can use them for.
My question is can I do this?
The STABLE-APIS.txt document is a bit vague and mentions the following
as Stable C++ API's in Android 1.5
cstddef
new
utility
stl_pair.h
Does that mean I can access them?
If so then how do I go about it? I dont think that following the
tutorials I have already done would be any help?
Any pointers on how to do this or links to tutorials etc.. would be
greatly appreciated
As others pointed out on the android-ndk group, you probably should just use the SDK. The NDK doesn't give you access to any features beyond those available with the SDK and it reduces the portability of your application. You should only consider it if you have legacy code written C or C++ (that doesn't use exceptions or RTTI). While some operations are much faster in native code, passing data between managed and native code is expensive and thus using the NDK only speeds up certain types of applications.
I want to create native Mac OS X application using Cocoa + Objective C but I need to connect to proprietary data source, and for this, owner of the data source only provides Java library. So I need to somehow import this Java library into my project and call functions on its Java classes.(Or create java wrapper around this library and then call my wrapper from objective-C).
Now, how can I do this? Quick google search leads me to JNI but I haven't found any good and actual(current) article/tutorial. I would really need some HOW TO article, how to load this java library, start VM if needed, and how to create java objects and call functions on them. Really something simple and I can move from there. Thanks.
Just to clarify, I repeat: I WANT to call Java functions from Objective-C, I do NOT want to call native functions from Java.
You're probably looking for the Invocation API, a little-known corner of Java Native Interface (JNI) which allows you to load the Java runtime in-process.
That said, you might have an easier time of it with a Java service application that communicates with your Objective-C application over network sockets.
You're looking for the Java-Objective C bridge, try looking at this article or on Apple's developer site. Be aware it is deprecated, that is it isn't being kept up to date with changes to Cocoa. But if you're just using it for an API passing standard Java datatypes you should be OK.