Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

[JNI]Can't call functions using JNI.

843829Mar 24 2010 — edited Jun 4 2010
I was wondering if anyone knows what I am doing wrong or can at least point me in the right direction. I am writing a native function that uses JNI to execute a cpp function. This I have working correctly, unfortunately when I try to call a cpp function from my cpp jni function, it crashed the JVM.

Below is a simplified version using printf, but I run into the crash when calling other functions as well.

When running the main from the cpp file everything works well and the string gets printed.

When calling the cpp printString function from java using jni it crashes the JVM. If I do not call an outside function (such as printf) then the jni works correctly and returns to the java just fine.

I have verified that as long as I don't call and outside cpp function that the jni code works correctly. I can call my own functions. I have even verified that the cpp code can do some math and return the resultant value to java. The only problem I am having is when calling a cpp function from my cpp code included in libraries I didn't write while using jni.

Below is all relevant files:

JniTest.java
package jnitest;

public class TestJni
{
    public static void main( String[] args )
    {
        System.loadLibrary("lib/JniTest");
        
        printString();
    }
    
    
    private static native void printString();
}
JniTest.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class jnitest_TestJni */

#ifndef _Included_jnitest_TestJni
#define _Included_jnitest_TestJni
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     jnitest_TestJni
 * Method:    printString
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_jnitest_TestJni_printString
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif
TestJni.cpp
#include <windows.h>
#include "TestJni.h"

void printIt()
{
	printf( "This is a string.\n" );
}

JNIEXPORT void JNICALL Java_jnitest_TestJni_printString(JNIEnv* env, jclass clazz)
{
	 printIt();
}

int main()
{
	printIt();
}
Compiler output
**** Full rebuild of configuration Release for project JniTestCpp ****

make -k clean all 
rm -rf  ./src/TestJni.o  ./src/TestJni.d JniTest.dll
 
Building file: ../src/TestJni.cpp
Invoking: GCC C++ Compiler
g++ -IC:\cygwin\usr\include\w32api -IC:\Program Files\Java\jdk1.6.0_18\include -IC:\Program Files\Java\jdk1.6.0_18\include\win32 -O3 -Wall -c -fmessage-length=0 -osrc/TestJni.o ../src/TestJni.cpp
Finished building: ../src/TestJni.cpp
 
Building target: JniTest.dll
Invoking: GCC C++ Linker
g++ -Wl,--add-stdcall-alias --kill-at -oJniTest.dll ./src/TestJni.o
Finished building target: JniTest.dll
 
Build complete for project JniTestCpp
Continued in next post.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 2 2010
Added on Mar 24 2010
7 comments
455 views