Acha Bhoot
Acha Bhoot

Reputation: 68

JNI can not find native method (test\Test.java:11: cannot find symbol)

i am following this tutorial on jni.

1) Steps made a test\Test.java file with method

public native static int getDouble(int n);

2) Compiled and generated a header file. (javac, javah)

3) Created a VC Win32 Project (Application Type: DLL)

4) Changed project properties to include

%JAVA_HOME%\include;%JAVA_HOME\include\win32\

5) Copy pasted test_Test.h in vc project.

6) Build > Confugration Manager (Changed platform to x64)

7) Build Solution + Copy resulting .dll file to Test.java Class path

8) Change Test.java to include call the the native funciton call.

package test;

public class Test {

    public native static int getDouble(int n);

    public static void main(String[] args) {
        System.loadLibrary("jni_example");

        for (int n = 1; n <= 20; n++) {
            System.out.println(n + " x 2 = " + getDoubled(n));
        }
    }
}

9) Trying compile Test again gives a problem .

D:\workspace\jni_example>ls
jni_example.dll  test  test_Test.h

D:\workspace\jni_example>javac -classpath . test\Test.java
test\Test.java:11: cannot find symbol
symbol  : method getDoubled(int)
location: class test.Test
                        System.out.println(n + " x 2 = " + getDoubled(n));
                                                           ^
1 error

When i comment out the System.out line it works ok ofcourse without printing anything.

D:\workspace\jni_example>java -version
java version "1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)

where am i going wrong with this?

Upvotes: 0

Views: 2198

Answers (2)

Sri Harsha Chilakapati
Sri Harsha Chilakapati

Reputation: 11940

You are getting the error because you wrote a typo. You are calling

System.out.println(n + " x 2 = " + getDoubled(n));

But you are declaring it like

public native static int getDouble(int n);

Notice the difference between getDouble and getDoubled.

Change the declaration to

public native static int getDoubled(int n);

This solves the problem.

Upvotes: 2

Mel Nicholson
Mel Nicholson

Reputation: 3225

This is a spelling error. getDoubled() versus getDouble()

Upvotes: 5

Related Questions