Question

I have some Java benchmarks with only class files.

I would like to find which benchmarks have JNI calls.

I thought maybe this can be done from the bytecode level with the help of javap -c, but not sure.

Any ideas?

Était-ce utile?

La solution 4

Thanks for all the answers.

I found a simple way as below:

java -verbose:jni -jar foo.jar

Basically, it prints a trace message to the console showing the JNI methods.

Here is the details about this option from HotSpot : ref

Autres conseils

If you're allowed to load the class, you can use reflection:

Class<?> clazz = ...
List<Method> nativeMethods = new ArrayList<>();
for (Method m : clazz.getDelcaredMethods()) {
    if(Modifier.isNative(m.getModifiers())) {
        nativeMethods.add(m);
    }
}

It is unclear from original question if you want to find native (JNI) methods programmatically. With javap you can use something like this:

javap -private java.awt.image.BufferedImage | grep native

This works along the lines you've described:

javah  [options] <classes> -d <dir>
grep -r "JNIEXPORT" <dir>

Each line of output will identify a native method using its JNI export name.

However, this does not determine if the native method has been called or loaded by the JVM or even defined in a shared library. A native method only needs to be defined if it is called and even then its absence is a trappable error.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top