Coder Social home page Coder Social logo

jnigi's Introduction

JNIGI

Java Native Interface Go Interface.

A package to access Java from Go code. Can be used from a Go executable or shared library. This allows for Go to initiate the JVM or Java to start a Go runtime respectively.

Docs: https://pkg.go.dev/tekao.net/jnigi

Pull Requests

Please make sure: go test works, add any doc comments for function signature changes.

v1

As of 2021-12-05 the master branch will be version 2. Packages that used JNIGI before this should update their go.mod to set v1 as the version. Or update their code to be compatible with version 2.

Compile

The CGO_CFLAGS needs to be set to add the JNI C header files. The compilevars.sh script will do this.

# put this in your build script
source <gopath>/src/tekao.net/jnigi/compilevars.sh <root path of jdk>

On Windows you can use compilevars.bat in the same way (but you don't need source at the begining).

Finding JVM at Runtime

Use the LoadJVMLib(jvmLibPath string) error function to load the shared library at run time. There is a function AttemptToFindJVMLibPath() string to help to find the library path.

Status

  • Has been used in Go (many versions since 1.6) executable multi threaded applications on Linux / Windows.
  • Tests for main functions are present.

Changes

  • 2022-07-29 Android support, see timob#55
  • 2021-12-05 Version 2: New idiomatic API. Converter interfaces. Add docs.
  • 2020-12-09 Add go.mod file, updated import path to tekao.net/jnigi.
  • 2020-08-21 Add ExceptionHandler interface for handling Java exceptions. Add 3 general handlers DescribeExceptionHandler (default), ThrowableToStringExceptionHandler and ThrowableErrorExceptionHandler.
  • 2020-08-11 Add DestroyJavaVM support, JNI_VERSION_1_8 const
  • 2019-05-29 Better multiplatform support, dynamic loading of JVM library.
  • 2016-08-01 Initial version.

Example

package main

import (
    "fmt"
    "tekao.net/jnigi"
    "log"
    "runtime"
)

func main() {
    if err := jnigi.LoadJVMLib(jnigi.AttemptToFindJVMLibPath()); err != nil {
        log.Fatal(err)
    }

    runtime.LockOSThread()
    jvm, env, err := jnigi.CreateJVM(jnigi.NewJVMInitArgs(false, true, jnigi.DEFAULT_VERSION, []string{"-Xcheck:jni"}))
    if err != nil {
        log.Fatal(err)
    }

    hello, err := env.NewObject("java/lang/String", []byte("Hello "))
    if err != nil {
        log.Fatal(err)
    }

    world, err := env.NewObject("java/lang/String", []byte("World!"))
    if err != nil {
        log.Fatal(err)
    }

    greeting := jnigi.NewObjectRef("java/lang/String")
    err = hello.CallMethod(env, "concat", greeting, world)
    if err != nil {
        log.Fatal(err)
    }

    var goGreeting []byte
    err = greeting.CallMethod(env, "getBytes", &goGreeting)
    if err != nil {
        log.Fatal(err)
    }

    // Prints "Hello World!"
    fmt.Printf("%s\n", goGreeting)

    if err := jvm.Destroy(); err != nil {
        log.Fatal(err)
    }
}

jnigi's People

Contributors

timob avatar iamcalledrob avatar mlaggner avatar andrewmostello avatar aniket144 avatar hscells avatar jairobjunior avatar edisoncxr avatar dbarganski avatar linguofeng avatar

Watchers

 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.