Steps for JNI calling in android to generate a header file

Source: Internet
Author: User

Steps for calling local methods through JNI in android: first, generate class files in eclipse

package Pag;public class JNI {static{System.loadLibrary("hello");}public native void  printHello();public static void main(String[] args) {new JNI().printHello();}}

The above example is a simple call of the native method in java. You can use javac to generate a. class file.

The first method is to enter the generated. class package directory (note that the directory containing. class is not the. class directory ).

For example:

C: \ Users \ Administrator> d:

D: \> cd workspace \ JNIText \ bin \ Pag
D: \ workspace \ JNIText \ bin \ Pag> javah JNI
Error: unable to access JNI
Incorrect class file:. \ JNI. class
Class file contains the wrong class: Pag. JNI
Delete the file or make sure the file is in the correct class path subdirectory.
Com. sun. tools. javac. util. Abort
At com. sun. tools. javac. comp. Check. completionError (Check. java: 164)
At com. sun. tools. javadoc. DocEnv. loadClass (DocEnv. java: 149)
At com. sun. tools. javadoc. RootDocImpl. (RootDocImpl. java: 77)
At com. sun. tools. javadoc. JavadocTool. getRootDocImpl (JavadocTool. java: 159
)
At com. sun. tools. javadoc. Start. parseAndExecute (Start. java: 330)
At com. sun. tools. javadoc. Start. begin (Start. java: 128)
At com.sun.tools.javadoc.Main.exe cute (Main. java: 66)
At com. sun. tools. javah. Main. main (Main. java: 147)
Javadoc: Error-fatal error
2 error


The above prompt is to enter the. class directory, not the package directory. Next, return to the package directory (D: \ workspace \ JNIText \ bin) in the previous directory. Compile the. h header file through javah Pag. JNI and generate the. h header file in the bin.

D: \ workspace \ JNIText \ bin \ Pag>
D: \ workspace \ JNIText \ bin \ Pag> cd ..

D: \ workspace \ JNIText \ bin> javah Pag. JNI

The bin contains the class package Pag, that is, the package path. Implemented using the javah command

The command for the second method is javah-classpath D: \ workspace \ JNIText \ bin-jni Pag. JNI (directly go to cmd) javah-classpath <包路径> -Jni <包含.class的包路径> .

Test C: \ Users \ Administrator> javah-classpath D: \ workspace \ JNIText \ bin-jni Pag. in JNI,-jni can be omitted without writing, but-classpath cannot be omitted; otherwise, an error is returned.

Javadoc: Error-invalid package name: "D: \ workspace \ JNIText \ bin" error.

Then, use the C \ C ++ programming tool to implement the method of its header file and generate the dll.

Reprinted with indicated source http://write.blog.csdn.net/postedit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.