See result of System.loadLibrary/ the defined methods and classes? - java

I have an .so file that I successfully integrated into android studio and I can load the file by calling System.loadLibrary.
Is there a way I can see what methods and classes I can use now with this library loaded?

Depends on how the library maps Java methods to native function pointers: it can do one or both of the following:
Dynamic symbols that start with Java_ are mangled names for the class and method name.
If the code uses RegisterNatives system call you will have to put a breakpoint on env->RegisterNatives and look at the surrounding code/arguments.

Related

How to access a method from a class defined in .DLL file from JAVA program?

I have a DLL file which has a class defined as 'Handler' which in turn contains method call getProperty(string,string).
How can I access such method from a JAVA program??
** I see a lot of examples on net which allows invocation of global function defined inside the DLL, but don't see any examples where we can invoke an method on a native object.
As mentioned in comments JNI is something you are looking for, but you will not be able to use "native" class/objects on Java side.
You'll need to add "native C" bridge layer with pure C API, which will wrap your C++ interface and translate calls to DLL, because only such API can be used on Java side via JNI.

How to add prefixes to jni dll function names

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.

Accessing functionality in JNI DLL through ColdFusion

I'm attempting to use the CreateObject function within ColdFusion to access functionality within a DLL through JNI. I've managed to get ColdFusion to load the DLL OK using the java.lang.System.load function, but am not sure how to then access the functions contained within this method.
My current code:
<cfset CreateObject("java","java.lang.System").load("C://Path//To//JniDll.dll")>
<cfset JniObject = CreateObject("java","Organisation.product.package")>
Which fails on line 2 with the error
Object Instantiation Exception.
Class not found Organisation.product.package
It's possible I'm accessing these methods incorrectly, but haven't been able to find any information about using JNI in ColdFusion on the web.
Thanks for your help,
Tom
You won't get access to the methods in that DLL just by registering it AFAIK. Either you get supplied a Java wrapper (probably as a JAR) which will then use the DLL, or you have to see what COM interfaces the DLL exposes and call them via createobject(type="COM"), as others have suggested.
From the looks of your code and error, you're expecting a Java class Organisation.product.package to be present and CF can't see it, so I'd take a look in the CF administrator and see which paths are in your classpath. Drop the JAR I assume you have into CF's lib folder and restart CF and see if that makes a difference.
I would suggest you make it work in java first. After that you place the generated jar file in the correct coldfusion lib directory and load (the java class) using CreateObject. The java class should be responsible for the dll binding.

Make package for Android

Problem Description
I'm writing module for Android (JAVA) that use native code (C). In JAVA I have created Class "MyClass.java" and implement all functions and classes there, also I call loadLibrary function from that file.
Now I want to build something like dll in C only for Android JAVA in order to give it to other users and they can use my class and functions.
You probably want to create a library project that contains your Java and C stuff.
http://developer.android.com/guide/developing/projects/index.html#LibraryProjects
Do you want it to be used at runtime or compile time?

Call Delphi's class from java

The question is whether can I create an instance of class (which is situated in .dll and has written in Delphi) and call method from it using Java.
I have found a lot of solutions about how to call native routines or methods in Java. What about classes?
One of the easiest is using JNA project. You'd have to create a set of functions in your DLL which could be called. Alternatively you can create a wrapper DLL if original cannot be changed

Categories