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.
Related
I am writing the python module where I need to interact with java module for some work.
I have already jar for java layer.
I can I invoke the jar files and call the class/Methods which are there in Jar files.
I don't want to use the Jython since major of my code is pure python
I tried subprocess.call()
but it's not serving my purpose
subprocess.call(['java','-jar', 'my.jar'])
EDIT:
I need to call the java layer because I need some input to my python module from there.
I tried py4j but no successes
JPype is an alternative to Jython that I made some good experience with. If it is not enough to call the java program and work with the output (it's hard to tell from your question), then JPype can be used to (more or less) transparently work with Java object in Python code.
I works by starting a JVM and handing requests to said JVM.
I want to bind a JAVA Swing GUI with C++ back end for a standalone application. These two form two different projects. I've read about Java Native Interface. Though JNI appears to be the best solution for this, I have few questions regarding JNI:
Can JNI help me bind different projects (one in Java swing and other
in c++)?
If yes, how will it be different from binding with the files in the same project?
Being a newbie in JNI, a detail technical help will helpful.
Yes .
JNI helps us perform calls to our own C++ library from java.
I'm guessing you are planning for the GUI and C++ to be on the same process (if yes JNI is the answer, if no, you will have to resort to intra-process communication).
You will have to bind dynamically projects from different languages.
Java is managed memory, meaning you have no true ownership over memory (The allmighty GC decides if he will delete or not something).
C++ is unmanaged memory, meaning you will have to pick up your own garbage.
With this difference being said, it's quite obvious that when interacting with eachother you need to be sure not to delete memory allocated in one from another.
This is one of the differences that you will bump into from binding files within the same project.
The other one is that you will resort to Dynamic linking.
The code frome Java is compiled into intermediate while the one from C++ is compiled into CPU specific assembler. Meaning you cannot use code from one into the other.
Hence we resort to Dynamic Linking.
The C++ code will be compiled into a shared library (.so or .dll depending on platform). At runtime, your java Swing application (which will be the entry point) will load the dynamic library into the program's memory (you will need to call this manually from your code, System.loadLibrary is the function if I remember correctly).
Once the library is loaded, if you built your library properly (for this you need to follow tutorials, I'm not going to explain that here), you will have the .dll exported symbols available in java .
The usual practice when using JNI is to create a bridge library that links a number of C++ functions to a bridge class in Java.
There are automatic programs that creates this bridge for you (such as SWIG), but I strongly recommend you avoid them (especially since you don't have a full grasp of JNI).
I have a compiler for a domain specific language that outputs a .net dll (that can then be used to do whatever the DSL said it should). It works well under .net but now I need to make the compiled dll functionality accessible from Java. The interface changes depending on the DSL, much like it would if I was implementing an F# type provider.
Ideally, I would like something along the lines of Reflection.Emit except that it would generate Java bytecode. It is important that the end-user can debug their Java code that uses my generated library from a Java GUI, so I don't think I can just use IKVM to instead include the Java code in .net. I also cannot use a commercial product such as JNBridge because all the users would need to install it, and call it every time their DSL code changes.
Is there a better solution than generating a .java file in text format and compiling it with a Java compiler? The .java file would just be a light-weight interface talking to the .net dll over some IPC mechanism (perhaps a named pipe), and its purpose would be to provide a typesafe interface similar to what would be seen from a .net application (except with indexers, getters, setters, overloads, etc replaced with some sort of horribly verbose syntax). Many thanks.
Java native interface is a way to call native code from Java (That dll isn't running on the JVM, thats for sure). Throw your dreams of "Write once run anywhere" in the bin now.
If you want to use dlls then you have to use JNI (or something similar or based on JNI like jni4net). It's not as pleasant as say, using c++ from c#, but it's pretty doable and far and away the best way to use dlls from java (I think the second best is re-writing the native code).
Alternatively you could run a windows process alongside your java app and soap/json/namedpipes/etc between the two, is that what you suggest in the last paragraph?
This would be odd for a small solution, but for a larger modular project it's not so crazy. I would recommend using sockets to communicate between the two, because I think sockets are easiest. It says client/server in the example, but it could be two processes (java and f#). ... This causes problems for performance, synchronisation, firewalls etc.. ahhh, just use JNI.
I need to find out which default windows program is executed when opening a text (.txt) file. For this purpose I want to call the FindExecutable function from windows.
How can this be achieved from Java?
I hope I do not have to make a JNI-implementation to call Windows via C for this purpose.
You will have to use JNI. If you don't to code it yourself, you can use JNA - I believe that JNA has moved to GitHub, but the documentation at the previous link is the best I could find. Note that you will be using JNI no matter what (and deploying a native dll), but JNA does make it a lot easier.
I have some dll files(not custom and not written by me) and I need to use the functions, that are c/c++ written, in these files in my java project. I googled and read many examples about JNI but they were all about writing your own program and dll and then reaching them. Also I don't think dllexport exists in these dlls, so dllimport/dllexport method is not available I guess.
How can I reach these functions?
Thanks in advance..
I'm sure, you looked at the JNI Tutorial at oracle already. I had a quick look at the part, where a native function is implemented and a dll is compiled and I don't think, that special conditions have to be met.
I'd give it a try with a single, easy function from that dll:
Write a simple class with just main method that uses one of the native methods (with easy parameters to have an easy start)
Generate the header file and
run the test application
If the dll is not 'jni compliant' (whatever that means), you'll know by then and then you probably know that you have to recompile the native code.