views:

70

answers:

4

Do they use something like Mono's PInvoke? Or is it more like internal calls registered before the runtime is started? Does java have a base library for handling native calls like mscorlib.dll? If I want to invoke a JVM in C code will it libraries look for the .so/.dll files? Does it make a difference to Java standard libraries if I statically link all of the JRE natives libraries?

+5  A: 

They use JNI, exactly as it is publicly documented, to invoke native shared libraries for the specific platform.

As far as invoking a JVM from C code, the JVM uses shared libraries (DLL, SO, etc). A quick search of the JDK 6 source code does not reveal any System.loadLibrary() for the core native support (like native methods in Object, String, etc). That suggests to me that the native code for these methods, which appears to be in DLL's judging from the contents of the JRE/bin directory, is explicitly linked by java.exe (and javaw.exe in Windows).

When I last looked at this stuff, the requirements for invoking a JVM from C code was a well documented part of JNI - I strongly suggest you refer to that doco to proceed further. We even went so far as successfully writing a native C wrapper/loader for the IBM AS/400 Java 1.1 JVM.

Software Monkey
+1  A: 

They use the Java Native Interface (JNI).

I've never called the JVM from C, so I don't know about that.

duffymo
A: 

There is an example here about how to start a JVM from inside your C program:

http://www.inonit.com/cygwin/jni/invocationApi/c.html

Thorbjørn Ravn Andersen
A: 

"Java Native Access (JNA) provides Java programs easy access to native shared libraries (DLLs on Windows) without writing anything but Java code" (quotation from their homepage).

Personally never tried it so far.

K. Claszen