I am not a Java developer, but I need to solve a problem: I need to include linux SO library to an existing Java project. The library is developed using CGO and works fine with C++, Python ctypes, Ruby FFI and so on. But I can not include it to Java project without of errors (java.lang.UnsatisfiedLinkError). I have read some articles like this, and the described method is that I need use javah to create a C header first, and then write a C program, and so on. But what to do, if I have already compiled *.SO file?
Is there a way, how to simply load an existing SO file (written on C) and call it`s functions?
Java doesn't have builtin FFI functionality.
One option for using native libraries (.dll/.so) with Java is to write a JNI wrapper library that has special functions that can be bound to Java native methods. That's the option where you need to use javah to create a header file for the wrapper library. See the "Java Native Interface" documentation on Oracle's site for how to do that.
The other approach is to use a "glue" library like JNA. You don't need to build another library this way but you need to include JNA in your project and do the necessary Java declarations for it. You can find the documentation for JNA in the Github repository together with the code. That approach is similar to what Python, Ruby, etc. are doing.
I recommend reading up on both to see what will better suit your needs.
Related
I have created a NuGet package (.Net standard 2.0 and .Net Framework 4.8 - compatibility) that I would like to know if it is possible to access from a Java application.
Nuget is a package format that is used in .NET. Not in Java. Java has Maven for that.
So there is no simple way (or any way I could recommend) to use the code easily in Java.
But if you really need to use the methods you have in the package, you can try to do the following:
Compile the code that the package contains, so that you have .dll file
Try to make use of the compiled dll in Java. I don't know much Java but it seems this is already answered in Stack Overflow: How to call a method in DLL in a Java program
Basically you should be able to call the methods defined in your DLL. Similar thing you can do in .NET. It is possible to take a dll, and call it's methods from .NET knowing the method signature. It is called Interoperability
Also please note that the nuget package is a zip repository in fact. You can open it using any zip tool, you can extract it and compile the code with dotnet command.
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.
I am trying to include a computational chemistry/physics visualizer known as Avogadro (1) inside of my jTabbedPane project. I used QT Jambi (1) to create a JUI (Java GUI) out of Avogadro's UI in Eclipse. I was successful in that attempt, but the GUI was (obviously functionless) upon running.
In short, the complication here is me trying to access the C++ libraries of a massive project that only has the ability to compile with CMake (Importing into Eclipse was not successful). To make it a bit worse, all I believe I need to interface with is a .cpp file that regulates the GUI and links it to the rest of the project, thus providing functionality. I plan to use SWIG to interface with the functions in the aforementioned cpp file and I plan for the interface to work with the C++ library to add functionality to the Java GUI.
Any thoughts on how I could do this? Do I need to SWIG the entire project?
It looks like Avogadro is open source. Therefore, you can download the source code and SWIG the entire project. This will give you the same classes in Java as you would have in C++.
If you only want a subset of Avogadro's functionality, I would suggest creating a simple bridge class in C++. This bridge class, with possibly other helper classes, could then be SWIGed. Your Java would now use the bridge class to execute the Avogadro library.
So... I will have a project which will be tested on Win 7 and some Linux server. It will be a web service that will use HSQLDB, Hibernate, Spring, Blaze DS and Flash (Flex RIA) as front end. I need to implement into it some image filtering\editing functionality which will be implemented in cross-platform C++ code (It will use Open-CV) wrapped in Java.
I need some kind of tutorial how to create cross-platform Java projects that use C/C++ libs *(most of all I am intrested in crossplatform compiling issue and what IDEs support such things)
It sounds like you'll benefit from the Java Native Interface. If you've got existing C and C++ code that you'd like to use from Java you may want to seriously consider something like GlueGen. It will save you a lot of time generating the code to access your C code.
You can have a look at the official Java JNI Examples here
Something along these lines?
http://www.javaworld.com/javatips/jw-javatip17.html?page=1
You're going to need to use JNI. The Java will be totally cross-platform and can be one project. For C++, you will need to create the JNI callable interface, and build as a dynamic library. The code should be pretty cross platform, but the actual build will be different.
On Linux, you need to build as a .so and you will probably use gcc. On Windows, you will probably use Visual Studio and build a .dll. The build will be different.
I'm wondering if a Java library can be called from a VB.net application.
(A Google search turns up lots of shady answers, but nothing definitive)
No, you can't. Unless you are willing to use some "J#" libraries (which is not nearly the same as Java) or IKVM which is a Java implementation that runs on top of .NET, but as their documentation says:
IKVM.OpenJDK.ClassLibrary.dll: compiled version of the Java class libraries derived from the OpenJDK class library with some parts filled in with code from GNU Classpath and IcedTea, plus some additional IKVM.NET specific code.
So it's not the real deal.
I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.
You can call Java from .NET if you wrap it in some form to make it accessable and the easiest way is typically to use a Runtime bridge like
http://www.jnbridge.com/
Other way is to wrap your API with java webservices.
check this also http://www.devx.com/interop/Article/19945
Nothing out of the box.
Most java/.net interop that I know uses web services.
If you can create COM components with Java, you can use tlbimp to create an interop assembly for using in VB.Net.
If can create standard DLLs that can be used from C++ with Java, you can write P/Invoke declarations and call them from VB.Net.
If you can create a web service with Java, you can generate proxy class from the WSDL and call it from VB.Net.
In any case, chances are the Java component will live in a separate process. I doubt you can load both the Java VM and the CLR in the same process.
If you have the source code and compile it using the J# compiler, then the answer is yes. If you want to call any pre-Java 2 (aka 1.2) libraries, then these are included pretty much verbatim with J#. More recent stuff is going to be tricky though (i.e., it's not there).
An example where this is used commercially are the yFiles graph layout algorithms from yWorks. These were originally just a Java library, but for the past few years they've been offering a .NET version, which is just the Java version compiled with Visual J#.
It's not without problems, and there are some limitations that you can't get around, but it can be done. So... unfortunately this answer looks pretty shady as well.
You could use JNI to instantiate a virtual machine and then use Java Classes. It will be some fun, though, because you would need to use C++ as a bridge between VB.Net and Java.
This article in java world has a quick tutorial on how to use Java from C++ and viceversa.
http://www.javaworld.com/javaworld/javatips/jw-javatip17.html
If you have the source, Visual Studio will let you convert Java code into c#.