How to use winapi functions in java? - java

I am doing a project in java which has the function to add files in database, and i want to use winapi functions to select files.
How would i do that?

By far the easiest way is by using Java JNA, it already has translation from stcalls in.
You can find a basic tutorial for it (with Winapi example no less) on JavaWorld.
You could also use JNI, but JNI requires you to use specially built header files for interfacing and it's all in all harder to maintain and use. It's slightly faster in some cases though.

You want Java Native Interface - this tutorial shows you how to implement your own but you could easily just point to WinAPI functions - http://java.sun.com/docs/books/jni/download/jni.pdf
Here's an example I found with a quick Google - not the neatest webpage but still demonstrates the concept well http://edn.embarcadero.com/article/20679

I strongly advise JACOB (Java COM Bridge) for using COM interfaces.
http://sourceforge.net/projects/jacob-project/
I wrote an app for getting song infos from Amazon Web Services. Its a nice library to have.

Related

What is the easiest way to call Objective-C code from Java?

I need to access a list of Wifi devices on Mac OSX from Java code and after researching it, I've found that I need to resort to "native" code - namely the CoreWLAN framework and the CWInterface.h class (https://developer.apple.com/library/mac/#documentation/CoreWLAN/Reference/CWInterface_reference/translated_content/CWInterface.html)
I initially thought I would be able to call this code using JNA but realized that I needed something since CoreWLAN is Objective-C (not C/C++).
I've tried this Objective-C Java bridge (https://github.com/shannah/Java-Objective-C-Bridge) but I can't work out how to make it find the CoreWLAN framework.
So I've also tried using JNAerator (https://code.google.com/p/jnaerator/wiki/ObjectiveC) so I can I use BridJ (https://code.google.com/p/bridj/), but I can't make it generate the right Java code.
Just using this style java -Xmx1000m -jar jnaerator.jar -framework CoreWLAN -jar CoreWlan.jar runs quickly but results in a jar that only contains mappings for CoreWlan.h
If I run jnaerator against CWInterface.h then jnaerator fails.
(I'm using the latest snapshot version of jnaerator)
What is the best way to call methods on CWInterface.h from Java?
What I would do is create a C++ class that communicates with the Java code through JNI. You can use both C++ and Obj-C in your xCode project. I haven't tried myself to use a Obj-C++ class with JNI, but I would just create a C++ class responsible for all communication between Java and Obj-C, this class can just be included in the Obj-C class where you need it (change the extension of this Obj-C file to .mm since it'll include C++ code).
I found this a helpful article on JNI: http://www.ibm.com/developerworks/java/tutorials/j-jni/
Personally I would do this via two totally separate programs that communicate via a shared memory mapped file. One program written in straight Objective-C, and the other in straight Java.
Memory mapping would mean that both programs would have access to the same region of memory, without having to resort to JNI at all.
Notes on memory mapping from Java:
http://javarevisited.blogspot.co.uk/2012/01/memorymapped-file-and-io-in-java.html
Notes on memory mapping from Objective C:
https://gist.github.com/jverkoey/2985830
That said, if you do want to do go the JNI route, then Apple has the following advice:
It is recommended that you use the Java JNI template in Xcode as a starting point for your JNI development.
To interoperate with the Objective-C runtime from JNI, link against JavaNativeFoundation.framework, which
is a sub-framework of JavaVM.framework. It contains Objective-C classes and macros to automatically set up
and tear down autorelease pools, catch and re-throw Java and Cocoa exceptions, hold JNI global references
in Foundation container classes, and convert object graphs of strings, numbers, lists, maps, and sets.
The above quotes were taken from here:
https://developer.apple.com/library/mac/documentation/java/conceptual/java14development/Java14Development.pdf
You can use the java objective-c bridge for this. You just need to load the framework you want to use with jna's Native.loadLibrary() method.
This example uses the WebKit framework. https://github.com/shannah/Java-Objective-C-Bridge/blob/master/java/test/ca/weblite/objc/TestWebView.java

android use pcap library

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.

JPen alternative for Java program

I want to develop a small Java program using some input like the pen of a Wacom graphictablet and found only the very active JPen project but are there some more possible alternatives without JNI or JNA (without any additional needed dll files), a pure Java implementation?
You can take a look at JTablet. From another SO thread.
It seems JPen moved to GitHub and this might be the best solution at the moment.
The other alternative solutions are not developed and maintained anymore.
https://github.com/nicarran/jpen

Any method for accessing Windows COM methods using Java?

I have an application which makes use of JNA to call functions from a hardware abstraction library in C++. One of the supported hardware devices requires Windows COM subsystem to be initialized (via CoInitialize or CoInitializeEx).
Does anybody know any Java library which can do the trick? I know I could wrap some Windows functions, but if I had something ready and tested, that would be better. I found one called JACOB but it is said that the documentation is not good and it's been a long time since they last updated it.
Thanks!
We use Com4J (for external processes like Excel) and the SWT COM API (for embedded GUI components). Sometimes, we use Jacob, it depends on the control you want to control. Sometimes they don't implement the IDispatch interface correctly, and then only one of the Java Com APIs works.
We also had COM components that needed a VB6 wrapper DLL, which in turn can be called with Com4J, so prepare for some headaches!
Have a look # JCom (Java-COM Bridge)
Other implementation exist but I used this one and it worked for me..

Calling Java library from Objective C on Mac

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.

Categories