using functions in dll, in java - java

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.

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.

Can you use Java Swing in c++

This may be a bit of an odd question, but I would like to know if you can use Java inside c++14. I don't really care for GTK (I find it confusing and over complicated). Swing, however, is very easy to use, and you can get a working project very quickly. So I would like to know if you can use Java Swing inside c++, so I can use Swing as the foreground, as in what you see like graphics, and use c++ for the background, as in stuff you cant see, like calculations and objects and stuff. So if I can have c++ code tell Swing what to look like, or when to update, that would be very useful for the project I have in mind. Thanks in advance for any advice I may receive.
EDIT: Being able to use c++14 inside Java would be acceptable as well. Also, if anyone could get me example code also, this would be very helpful. Thanks!
You shouldn't run Java from C++, but rather C++ from Java. Oracle gives you a way to load native shared libraries, using JNI.
So you would create your view in Java, using Swing, then you would update your view by calling C++ functions that were pre-compiled and exported in a shared library.
That said, using JNI is quite tricky and the speed improvements of C++ might not be worth it; so you should consider using only Java (or only C++ and a library to create your GUI, such as Qt)
In which direction you go is a matter of taste (loading the JVM from a C++ program or loading DLLs from the JVM side).
Usually you go the way which is more logical, e.g. if you already have a C++ program you likely want to load the JVM from the C++ side. That would be your case. Especially if the Java you want to add is essentially "scripting" the C++ application.
If you already have a Java program and want to access a C++ DLL, you load the DLL from Java and write a simple JNI / native Java class.
In our times you would use tools like JNA for that (instead of JNI): https://github.com/java-native-access/jna
Or you can use SWIG to generate wrappers for your C++ classes: http://www.swig.org/

Call FindExecutable in Java

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.

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

Combining Java and C without gcj -- move C to Java or Java to C?

First, I have no experience doing this. But like the beginning of any good program, I have problem that I need to fix, so I'm willing to learn.
So many of you are probably already familiar with pdftk, the handy utility for handling various pdf-related tasks. So far as I can tell, most of these features are available in much newer, lighter libraries/extensions, except the one I need (and probably the only reason it still exists): merging form data files (fdf and xfdf) with a form PDF and getting a new file as the output.
The problem is that my server doesn't have gcj, which is fundamental to build/compile pdftk. I don't know if it's because I'm on Solaris or if it's for some other sysadmin-level reason, but I'm not getting gcj anytime soon. And there are no pre-compiled binaries for Solaris as far as I can find.
So I'm thinking that the MAKE file and C code can be rewritten to import the Java library (very ancient version of itext) directly, via javac.
But I'm not sure where to really start. All I know is:
I want a binary when I'm done, so that there won't be a need for a Java VM on every use.
The current app uses GCJ.
So my first thought was "Oh this is easy, I can probably just call the classes with some other C-based method", but instead of finding a simple method for doing this, I'm finding tons of lengthy posts on the various angles that this can be approached, etc.
Then I found a page on Sun's site on how to call other languages (like C) in a Java class. But the problems with that approach are:
I'd have to write a wrapper for the wrapper
I'd probably be better off skipping that part and writing the whole thing in Java
I ain't ready for that just yet if I can just import the classes with what is already there
I'm not clear on if I can compile and get a binary at the end or if I'm trapped in Java being needed every time.
Again, I apologize for my ignorance. I just need some advice and examples of how one would replace GCJ dependent C code with something that works directly with Java.
And of course if I'm asking one of those "if we could do that, we'd be rich already" type questions, let me know.
I'm not sure what you are looking for exactly, so I provided several answers.
If you have java code that needs to run, you must:
Run it in a jvm. You can start that vm within your own custom c-code, but it is still using a jvm
Rewrite it in another language.
Compile with an ahead-of-time compiler (eg gcj)
Incidentally, you could compile a copy of gcj in your home folder and use that. I believe the magic switch is --enable-languages=java,c (see: here for more)
If you have c-code you want to call from java, you have four options:
Java Native Interface (JNI). It seems you found this
Java Native Access (JNA). This is slower than JNI, but requires less coding and no wrapper c-code. It does require a jar and a library
Create a CLI utility and use Runtime.Exec(...) to call it.
Use some sort of Inter Process Communication to have the Java code ask the c-code to perform the operation and return the result.
Additional platform dependent options
Use JACOB (win32 only: com access)
I am not sure if I understand what you are looking for.
If you are looking to incorporate the C code into Java to make a native binary without the gcj, I think you are out of luck. You can include the C in Java, but it would be a primarily Java program meaning you would need the JVM on each run. Is there anything stopping you from compiling the gcj yourself?

Categories

Resources