开发者

Call JNI methods that were not written for your class

开发者 https://www.devze.com 2022-12-22 06:10 出处:网络
It seems that JNI methods need to be written with开发者_如何学C the Java class signature built in to them.I want to call a JNI method that I didn\'t write.Can I call a native method on a library that

It seems that JNI methods need to be written with开发者_如何学C the Java class signature built in to them. I want to call a JNI method that I didn't write. Can I call a native method on a library that was not written with my class in mind?


Given:

package com.mycompany.package

class MyClass
{
public native void doSomething();
}

is transformed (mangled) into a natively linked method name of:

Java_com_mycompany_package_MyClass_doSomething

it seems you would need the same class in the same package in order to connect the native method to the Java method. This seems like it's likely to be problematic and/or error prone if you don't have control over the native library. And packaging your code in a foreign package is a bad idea, esp. if the foreign package is signed and sealed.

Also problematic is that native invocation generally have some specific considerations which you might be violating - like thread-safety concerns, or releasing memory or lock (the need to call other native functions around the one you're using).

From my (considerable) experience with JNI I wouldn't recommend it unless you're desperate and have no other options, and you're really familiar with the target system and API.

Your better option would be to write a simple native wrapper around the target O/S API(s) you want to use.

0

精彩评论

暂无评论...
验证码 换一张
取 消