I have dll file which is controlling a device, I need a tool to generate java classes which take the dll file as an input and generate all the need JNI classes.
Note: I don't have the C,C++ code for that DLL.
I believe you are doing all this so that you can call C++ methods through Java.
Instead use JNA library. It does not require any explicit JNI classes to be built. You can directly call C++methods. Read more about it here
Related
I try to use a .NET Library in Java. Therefore, I use JNI calls of a C++ project which refers / uses a DLL of the .NET.
To use the .NET Library I have several steps in-between. First I use C# project as a ‘wrapper’ instance between the Library and a C++ project. The C# project creates a DLL.
The main wrapper C# class (CSHARPWRAPPERSession) is [ComVisible(true)] and has a fixed GUID [Guid("XXX")]
Out of this ‘C#’-DLL I create a TLB file.
This tlb file is imported by my C++ project.
#import ".\CSHARPWRAPPER.NET.tlb" no_namespace, named_guids
When I create an exe out of the C++ project I can use the wrapper perfectly and call the needed .NET library.
Now I try to make a connection between the C++ project an JAVA. Therefore, I use JNI and created DLL out of the C++.
This leads to the error 0x80040154 – Class not registered.
This error happens, when I try to call CoCreateInstance(CLSID_CSHARPWRAPPERSession)
I have registered all used Libraries with regasm and done a gacutil on it.
What am I doing wrong? Any ideas or suggestions?
I have a jni dll that has functions being called from java. The problem is that this dll has all the java classes in the default package (in the dll "Java_classname_methodname"). It is impossible to get the source of this dll and it would take EXTREMELY long to rewrite. So I basically need to call the functions in this dll from java in a different package than default. I've tried for hours on end to rename the functions in the dll with a hex editor and several tools to modify the checksum and addresses in the dll but it's just too much for me because I have almost no experience with this. I would very much prefer this route, but I just don't have the proper tools or the know-how. So what I'm left with is trying to hardcode the package name in java. I tried using jna as described in this stack overflow post to do something like this:
Map options = new HashMap();
options.
put(
Library.OPTION_FUNCTION_MAPPER,
new StdCallFunctionMapper() {
public String getFunctionName(NativeLibrary library, Method method) {
method.setName(method.getName().replace("com.test.", "");
return super.getFunctionName(library, method);
}
}
);
Native.loadLibrary(..., ..., options);
But there is no setName in Method. Ideally I'd like to get this done without any extra libraries but I'm obviously not opposed to using something like jna. And before anyone asks, yes this is permitted by the library's usage license. Please don't tell me it's not possible because know that it's possible, just difficult. Whichever way it must be done I am willing to put in the work (either modifying dll or using java code with some external library). And by the way, I also need this done on .so and .dylib files eventually (not as important as dll). Thank you for your time.
I have a JNI dll that has functions being called from java. The problem is that this DLL has all the java classes in the default package (in the dll "Java_classname_methodname").
So the corresponding Java class with the native methods wasn't in a package either.
It is impossible to get the source of this dll and it would take EXTREMELY long to rewrite. So I basically need to call the functions in this dll from java in a different package than default.
Correct.
I've tried for hours on end to rename the functions in the dll with a hex editor and several tools to modify the checksum and addresses in the dll but it's just too much for me because I have almost no experience with this.
You may be able to alias the function names somehow, but it's been about 20 years since I practiced in this area.
I would very much prefer this route, but I just don't have the proper tools or the know-how. So what I'm left with is trying to hardcode the package name in java.
No. There is no package name to hard-code. What you're left with is trying to call native methods in a class without a package from a class in a package, which since 1.4 is not possible.
What you need to to do is either:
It is possible to call the package-less Java native methods via reflection. So, you can write a wrapper class that does that, which lives in the package of your choice, and the rest of your code can call the wrapper.
Or
As follows:
Write another Java class in the package of your choice with the appropriate native methods.
Create an import library from the existing DLL.
Write another DLL and link it with this import library.
Load this new DLL from your Java code.
Have this DLL use the RegisterNatives method on initialization, to register the existing JNI entry points in the old DLL under the new native method names & signatures.
Use javah to get the required native method names and javap to get the required Java signatures. Don't try to guess them by hand.
Don't ask me how this maps to .so files.
I have an existing .c and .h file from a library. I would like to be able to call the functions contained within the c files from Java. I understand that this requires the JNI, and I have a pretty good understanding of how I will need to modify the .c and .h files in order to make them work with the JNI. What I don't fully understand how to do, or if it's even the purpose of the JNI, is to make a simple JNI library that I can then wrap into another java application. I want to create the interface; extend the C API into Java so I can use it there. The final result would be something like: someCmethodInSomeJNILibrary() located in some jnilib sort of file.
Thanks in advance
First, you may wish to consider using JNA. JNA would allow you to interact without writing additional native code. This is particularly convenient if you already have a library and don't want to build another.
With JNI, the steps required include:
Design a Java API of native methods for interacting with the C code.
Use javah to generate C stubs for the Java native methods.
Glue the C stubs to your C code.
Generate a dynamic/shared library containing the native method functions and your original C code.
Load that shared library in Java.
More detail can be found in "Compiling, Loading, and Linking Native Methods" in the JNI documentation.
I'm writing a C++ dll (file extension in Visual Studio is .cpp but the code is C) which uses JNI for loading and calling instance methods of some Java classes. As a result my project is made up by C++ source and header files plus some Java class files (the class files for the Java objects used through JNI). I would like to create a single dll library including also the Java class files "in a single bundle". Is it possible to put both the C and Java files into the dll?
This is perfectly possible, though not necessarily intuitive. I haven't tested this but I would imagine it would work easily if you knew what you were doing.
First off, you're going to need a way to package the class file's binary into the native binary you own, and then of course be able to seamlessly read that chunk at any point in time. How you achieve this is up to you, there is no real 'right way' to do it.
That being said, if you can get a handle to the memory where the .class file's data resides (after loading the DLL) and pass it to Java as a byte[] or ByteBuffer, you'd then want to use any one of ClassLoader's defineClass overloads along with a call to findLoadedClass and then finally loadClass.
This would allow you to load in a class from virtually any data source. As I mentioned before, how you store/retrieve the native data for the class is up to you.
I have an applet which contains some images, regular java classes and class, which uses C code via JNI. All this is included in jar file, and the appropriate dll is located somewhere on a harddrive. In C code I have to read a bitmap from the applet(jar). I know that it is simple to access this file from java via getResourceAsStream() method, but can I do it from native code?
I understand that I can pass the bytes of the bitmap to native code as a workaround, but it requires some rewriting, so I would like to avoid this.
So, is there any way to access files from within jar via native code, executed by class belonging to this jar?
Just call the appropriate Java methods.