Java Learning-writing Java JNI call __java in ubuntu command line mode

Source: Internet
Author: User

Call the C program through JNI to implement "Hello world!" Output, mastering this method makes it easy to implement Java functions in C/C + +.

To determine the successful installation of Java, on this machine because of engineering needs, has installed several JDK versions, the implementation of Java-version

root@wang:~/jni# java-version
OPENJDK Version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-0ubuntu4~14.04-b14)
OpenJDK 64-bit Server VM (build 25.91-b14, Mixed mode)

You can see the version using OpenJDK 8.

Writing can helloworld.java a program, there is a native method declared as Displayhello.

public class HelloWorld {
	static {
		system.loadlibrary ("Hello");
	}
	public native void Displayhello ();
	public static void Main (string[] args) {
		new HelloWorld (). Displayhello ();
	}
Execute javac helloworld.java generate Helloworld.class file

Executing javah-jni helloworld will generate HelloWorld.h files in the current directory

  /* Do isn't EDIT this file-it is machine generated/*
  #include <jni.h>/
  * Header for class HelloWorld */
  
    > #ifndef _included_helloworld
  #define _included_helloworld
  #ifdef __cplusplus
  extern "C" {
  #endif
   * * Class:     HelloWorld
   * method:    Displayhello
   * Signature: () V
  * * Jniexport void Jnicall Java_helloworld_displayhello
    (jnienv *, jobject);
  
  #ifdef __cplusplus
  }
  #endif
  #endif
  
This file is automatically generated for Javah and does not need to be modified to see a function declaration containing a java_helloworld_displayhello.

Write a IMP_HELLO.C program that implements the function just declared.

#include <jni.h>
 #include "HelloWorld.h"
 #include <stdio.h>
 jniexport void Jnicall Java_ Helloworld_displayhello (JNIENV * env, Jobject obj) {
     printf ("from imp_hello.c:");
     printf ("Hello world!") \ n ");
     return;
 }
This file includes two header files jni.h and generated HelloWorld.h, and the implementation function prints a WORD from Imp_hello.c:hello world!

To generate a dynamic library command:

Gcc-shared-fpic-i/usr/lib/jvm/java-8-openjdk-amd64/include/-i/usr/lib/jvm/java-8-openjdk-amd64/include/linux/ Imp_hello.c-o libhello.so

Different machines may not have the same path to the JDK, and you can view the JDK version using the above command, which contains two file paths, one is the jni.h file path, and one is the Jni_md.h file path. If you encounter other errors, you can be prompted to solve, such as on this machine if you do not include-fpic, you will be prompted to error

root@wang:~/jni# gcc-shared-i/usr/lib/jvm/java-8-openjdk-amd64/include/-i/usr/lib/jvm/java-8-openjdk-amd64/ Include/linux/imp_hello.c-o libhello.so
/USR/BIN/LD:/tmp/ccxqftfa.o:relocation r_x86_64_32 against '. Rodata ' can not is used when making a shared object; Recompile with-fpic
/tmp/ccxqftfa.o:error adding Symbols:bad value
Collect2:error:ld returned 1 exit status
The tip has been given a solution, recompile with-fpic, if you encounter other errors can also be connected to the Internet to inquire about the problem.

Build libhello.so library under current directory after compilation completes

root@wang:~/jni# ls
Helloworld.class HelloWorld.h Helloworld.java imp_hello.c libhello.so

Final implementation

root@wang:~/jni# java-djava.library.path=. HelloWorld
From Imp_hello.c:hello World!
At this time the Java program has successfully called the C program, if it is a C + + program can be named Imp_hello.c Imp_hello.cpp, the GCC into a g++, the other steps and the same.


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.