Go Examples of using JNI development under Ubuntu

Source: Internet
Author: User

Http://www.cnblogs.com/zxglive2006/archive/2012/01/15/2323110.htmlCreate Java Project with Eclipse first;then add the Prompt.java file directly to the project and put it under the default package (it is best not to add the packages, otherwise error-prone). 1. Write a Java file in which you declare the native method and load the dynamic-link library through a static statement block, with the example Prompt.java code as follows: Class Prompt {Private native String getLine (string p     ROMPT);        public static void Main (String args[]) {Prompt p = new Prompt ();        String input = P.getline ("Type a Line:");    System.out.println ("User typed:" + input); } static {system.loadlibrary ("Prompt");//Here to so library name must not be mistaken, prompt corresponding to the actual library name is libprompt.so    }} 2. Call the Javac command to generate the Prompt.class file; Javac prompt.java3. Call the Javah command to generate the Prompt.h header file for the C program reference: JAVAH-JNI Prompt Auto-generated header files are as follows:/* Do not EDIT This file-it are machine generated */#include <jni.h>/* header for class Prompt */& nbsp, #ifndef _included_prompt#define _included_prompt#ifdef __cplusplusextern "C" {#endif/* * Class:     prompt * Method:    getline * Signature: (ljava/lang/string;) ljava/lang/string; */ Jniexport jstring jnicall java_prompt_getline   (jnienv *, Jobject, jstring);  #ifdef __cplusplus} #endif #endif4. Writing the prompt.c file for specific functions: #include <jni.h> #include <stdio.h> #include "Prompt.h"  jniexport void Jnicalljava_prompt_getline (jnienv *env, Jobject obj, jstring Prompt)  {    char buf[128];     const jbyte *str;    str = (*env)->getstringutfchars (env, prompt, NULL);   & Nbsp;if (str = = null) {        return null;   &nbsp        }    printf ("%s", str);     (*env) Releasestringutfchars (env, prompt, str);    scanf ("%s", buf);    return (*env), Newstringutf (env, BUF);} 5. Compile the dynamic library libprompt.so; The following error may be reported:

/USR/BIN/LD:/tmp/ccg1iykj.o:relocation r_x86_64_32 against '. Rodata ' can not is used when making a shared object; Recompile with-fpic
/tmp/ccg1iykj.o:error Adding symbols: Wrong value
Collect2:error:ld returned 1 exit status

Plus compile option-fpic; gcc-shared-fpic-i/usr/lib/jvm/java-6-sun-1.6.0.26/include-i/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux Prompt.c-o Libprompt.so (gcc-shared-fpic-i/opt/java/jdk1.8.0_71/include/-i/opt/java/jdk1.8.0_71/include/linux/prompt.c-o libprompt.so) 6. Run. Java prompt may report the following error

Exception in thread "main" Java.lang.UnsatisfiedLinkError:no test in Java.library.path
At Java.lang.ClassLoader.loadLibrary (classloader.java:1864)
At Java.lang.Runtime.loadLibrary0 (runtime.java:870)
At Java.lang.System.loadLibrary (system.java:1122)
At Test.<clinit> (test.java:14)

You need to set the path of the Java.library.path to execute:

Java-djava.library.path=. Prompt

Go Examples of using JNI development under Ubuntu

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.