I have a .dll that was provided, and I need to interface with it using java.
Do I have to rewrite the C to use the JNI types provided in jni.h? as seen in java sun's jni example.
Otherwise, How do i declare the native function my java function to pass and receive pointers?
thanks
You may find JNA useful
"JNA provides Java programs easy access to native shared libraries (DLLs on Windows) without writing anything but Java codeāno JNI or native code is required. This functionality is comparable to Windows' Platform/Invoke and Python's ctypes. Access is dynamic at runtime without code generation."
In a way, yes.
Most of the time you would just write a small "wrapper" dll with functions that would do the type conversions and delegate to the corresponding functions in the "real" DLL.
You can rewrite the C code of course, but it's not uncommon to write a wrapper (technically, using the Facade or Bridge pattern) for the C code. You write code that matches your expectations in the Java, and have that code call your existing C code.
Related
I am trying to implement the following:
Compute something in Java and return value to Fortran.
I implemented it using JNI and the sequence is as follows:
Fortran 95 -> C -> Java (Computes something)
Java (returns value to Fortran 95) -> C -> Fortran95
Can this be achieved using JNA? If so, what are the advantages?
Fortan's shared libraries are compatible with C, so JNA should be able to access any interfaces exported, including those that have callback inputs.
The advantages of JNA over JNI:
Speed of development You don't have to configure or compile any native code, which can significantly speed up your development/build cycle.
Clarity JNA's mappings look just like the functions you'd be calling in native code, but you get to use Java semantics instead of translating Java into C/JNI, and then C/JNI into appropriate wrappers around Fortran
Maturity of Code JNA has already worked out the gotchas and details of managing the native interface, so you can focus on solving your real problems instead of doing plumbing. However, if you like to do plumbing, JNI is always there.
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).
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.
I am able to call C++ code from Java using SWIG but I can't find any documentation on how to do the reverse (call Java from C++).
The official SWIG documentation says (http://www.swig.org/Doc1.3/Java.html#java_overview):
"SWIG enables a Java program to easily call into C/C++ code from Java. Historically, SWIG was not able to generate any code to call into Java code from C++. However, SWIG now supports full cross language polymorphism and code is generated to call up from C++ to Java when wrapping C++ virtual methods."
But I can't find where it says how to do it! Any help is greatly appreciated. Thank you.
Actually this is possible and I have implemented it based on inheriting a virtual class from C++ to Java.
You can find solution in chapter "24.5 Cross language polymorphism using directors" of Java part of SWIG document.
Based on the rather strange wording of the statement I'd say what you want isn't generally possible. "...call up from C++ to Java when wrapping C++ virtual methods." The "...call up..." leads me to believe you're calling protected or public members of an inherited interface and, "...when wrapping C++ virtual methods," leads me to conclude you can only do so when you're overriding an inherited interface. So it sounds like the actual use case is very narrow.
But then, I'm just basing this on the wording of the text you've pasted. It may or may not help you.
There is a tiny official example on calling Java callbacks from C++. Works well.
Your C++ callback class better be non-abstract, you gonna loose default constructor in Java otherwise.
Does anyone know of a way to issue commands to a hard drive within Java? Does Java even support this kind of hardware interaction?
For example, if I have a SCSI hard drive that I would like to inquiry, is there a pre-existing Java method to do this, or would I have to write my own?
http://en.wikipedia.org/wiki/SCSI has some general information on SCSI commands in case you aren't familiar.
Java doesn't support talking directly to hardware like that. However, you can use JNI to call a C/C++ function from Java that can.
Three words "JNI or JNA". I strongly recommend taking a look at the latter to see if it suits your situation, instead of just opting for JNI.
No, since Java runs in a "virtual" machine rather than a real one. But it could be used as a bridge as dj mentioned earlier using JNI.
According to Wikipedia JNI can also call assembly directly. JNI could be used to call complete programs written in C or C++
you need to write the HDD interface code in C/C++ and then call that from Java using JNI