Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know whether my Java application uses "native code"?

On a recent question, I got comments asking whether I was using "native code" in my application. Now, I know that there is some way to call code in traditional binary libraries (DLLs, SOs) from inside a Java application using a thing called "JNI". I have read that Wikipedia entry but I never used this.

I am using a number of libraries, some of which may or may not use native code. How do I find out if they do? I did not have to install any SOs (running on Linux), but I guess that doesn't mean the libraries are not using any? Do I have to browse through all the documentation (which varies greatly in quality between libraries) or can I do some analysis on the JARs?

like image 314
Hanno Fietz Avatar asked Sep 14 '11 08:09

Hanno Fietz


People also ask

What is native code in Java?

Native code compiler for Java translates the Java code into a binary representation that can be linked to precompiled library files and resources to create an executable program. Native code compilers eliminate the need for JVM and interpreters to convert the Java byte code, which is a portable intermediate code.

Does Java run natively?

This chapter introduces the Java Native Interface (JNI). The JNI is a native programming interface. It allows Java code that runs inside a Java Virtual Machine (VM) to interoperate with applications and libraries written in other programming languages, such as C, C++, and assembly.

Can you compile Java to native code?

Yes! Native Image The native image feature with the GraalVM SDK helps improve the startup time of Java applications and gives them a smaller footprint. Effectively, it's converting bytecode that runs on the JVM (on any platform) to native code for a specific OS/platform — which is where the performance comes from.


1 Answers

Usually using libraries that require JNI requires some additional setup (like putting .dll or .so files in the right places or setting the java.library.path System property).

If you did nothing of that, then chances are that you're not using JNI anywhere. This is also somewhat likely, as only quite specialized libraries require JNI.

However there's also JNA, which is a wrapper around JNI which simplifies its usage and which sometimes makes it unnecessary to do any explicit setup. If that's used by one of your libraries, then it's harder to detect.

If you get a crash dump, then checking that for any non-standard libraries can give a hint if a user-loaded native library is to blame.

like image 53
Joachim Sauer Avatar answered Sep 30 '22 13:09

Joachim Sauer