Giter Site home page Giter Site logo

jni's Introduction

jni

方便集成 JNI 代码的 Go 库。

完整支持所有 JNI 功能函数:https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html

配合 https://github.com/ClarkGuan/gojni 使用,方便 Java 与 Go 代码通信。

安装

安装前准备:

  • 安装 JDK,并设置 JAVA_HOME 环境变量
  • 安装 includejni ( go get -u github.com/ClarkGuan/includejni )
  • 运行下面命令
git clone https://github.com/ClarkGuan/jni
cd jni
./build.sh

使用举例

我们假设您已经安装 JDK、Go 以及 C 编译器,这里以 MacOS 平台为例:

  • 目录结构
.
└── src
    └── java
        └── com
            └── demo
                └── Main.java
  • 编写 Java 源代码
package com.demo;

public class Main {
    static {
        System.loadLibrary("hello");
    }

    public static void main(String[] args) {
        nativeHello();
        System.out.println(stringFromJNI());
    }

    private static native void nativeHello();

    private static native String stringFromJNI();
}
gojni src/java/com/demo/Main.java
  • 此时在目录中生成文件 libs.c 和 libs.go,我们修改 libs.go 文件内容如下:
package main

//
// #include <stdlib.h>
// #include <stddef.h>
// #include <stdint.h>
import "C"
import (
	"fmt"

	"github.com/ClarkGuan/jni"
)

//export jniOnLoad
func jniOnLoad(vm uintptr) {
	fmt.Println("JNI_OnLoad")
}

//export jniOnUnload
func jniOnUnload(vm uintptr) {
	fmt.Println("JNI_OnUnload")
}

//export jni_com_demo_nativeHello1
func jni_com_demo_nativeHello1(env uintptr, clazz uintptr) {
	fmt.Println("native hello form golang")
}

//export jni_com_demo_stringFromJNI2
func jni_com_demo_stringFromJNI2(env uintptr, clazz uintptr) uintptr {
	return jni.Env(env).NewString("This is string from Golang code!!!")
}
  • 运行 go build 生成 Mac 上运行的动态库文件:
go build -buildmode=c-shared -ldflags="-w -s" -v -x -o libhello.dylib

注意:需要将 JNI 的头文件引入,否则 C 编译器可能找不到。使用环境变量 CGO_CFLAGS:

CGO_CFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/darwin" go build -buildmode=c-shared -ldflags="-w -s" -v -x -o libhello.dylib

或者使用前面下载的 includejni 工具:

includejni go build -buildmode=c-shared -ldflags="-w -s" -v -x -o libhello.dylib
  • 编译 Java 源码
javac src/java/com/demo/Main.java
  • 将 libhello.dylib 放入 Java 虚拟机可以找到的位置(通过 Java 环境变量 java.library.path 指定)

  • 运行 Java 程序

java -cp src com.demo.Main
  • 运行结果
JNI_OnLoad
native hello form golang
This is string from Golang code!!!

jni's People

Contributors

clarkguan avatar wdsgyj avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.