Céline Muller
Céline Muller

Reputation: 11

I am getting trouble when I use command java -jni HelloWorld

I am learning how to use jni(java native interface), and I follows steps presented by a blog. I have created a java file named 'HelloWorld.java',content as following:

public class HelloWorld { 
    public native void displayHelloWorld(); 
    static { 
        System.loadLibrary("HelloWorldImpl"); 
    } 
    public static void main(String[] args) 
    { 
    // TODO Auto-generated method stub 
        HelloWorld helloWorld = new HelloWorld(); 
        helloWorld.displayHelloWorld(); 
    } 
}

then I execute this command:

javac HelloWorld

there's no error happened, but when I execute this command:

javah -jni HelloWorld

then I get a error:

can't find class "HelloWorld"

I am sure, this directory has the HelloWorld.class file that have been compile.

the dev:

jdk8
windows 10 64bits

I have google for a long time and asked my classmates who are successful using same steps, but cannot deal with this problem, something was wrong in my laptop? anyone could help me?Thank a lot.

Upvotes: 0

Views: 275

Answers (1)

Oo.oO
Oo.oO

Reputation: 13375

First of all, make sure to use packages. It is not mandatory, but it simplifies things.

Then, after you have compiled your java code, make sure to use:

javah -jni -cp . HelloWorld

You can alternatively create header files in some location

javah -jni -d c -cp . HelloWorld
# -d c     -> header files will be created inside directory called "c"

I'd also suggest to compile classes into some subdirectory as well:

javac -d target HelloWorld.java
# compiled classes will be inside "target" dir
# then, you can call javah this way
javah -jni -d c -cp target HelloWorld

Take a look here for full sample with super easy code:

http://jnicookbook.owsiak.org/recipe-No-001/

Have fun with JNI!

Upvotes: 1

Related Questions