All Projects → timob → jnigi

timob / jnigi

Licence: BSD-2-Clause License
Golang Java JNI library

Programming Languages

go
31211 projects - #10 most used programming language

Labels

Projects that are alternatives of or similar to jnigi

jellyfin-sdk-kotlin
Kotlin SDK for Jellyfin, supporting Android and JVM Targets
Stars: ✭ 43 (-53.26%)
Mutual labels:  jvm
tribble
Coverage based JVM Fuzz testing tool.
Stars: ✭ 16 (-82.61%)
Mutual labels:  jvm
delta
DDD-centric event-sourcing library for the JVM
Stars: ✭ 15 (-83.7%)
Mutual labels:  jvm
LGP
A robust Linear Genetic Programming implementation on the JVM using Kotlin.
Stars: ✭ 14 (-84.78%)
Mutual labels:  jvm
Hephaistos
NBT & Anvil save format library
Stars: ✭ 22 (-76.09%)
Mutual labels:  jvm
JavaInterview
JVM、JUC(高并发)、集合、计算机网络、数据库、MySql、Redis、、工作流(Activiti)、规则引擎(Drools)、Spring、SpringCloud、Mybatis、Git、Docker、Utils、Linux
Stars: ✭ 179 (+94.57%)
Mutual labels:  jvm
linux.gpio.clj
Use the standard Linux GPIO API from Clojure JVM
Stars: ✭ 24 (-73.91%)
Mutual labels:  jvm
iokk
International Obfuscated Kotlin Contest
Stars: ✭ 56 (-39.13%)
Mutual labels:  jvm
hsdis-macos
macOS Build artifacts for hsdis HotSpot Plugin
Stars: ✭ 22 (-76.09%)
Mutual labels:  jvm
java-rust-ffi
🍋 FFI example for accessing Rust lang dynamic libraries from java
Stars: ✭ 37 (-59.78%)
Mutual labels:  jvm
tools jvm autodeps
Automatic Dependency Management Tools for JVM Languages
Stars: ✭ 48 (-47.83%)
Mutual labels:  jvm
sherlock-distributed-lock
Distributed locking library for JVM
Stars: ✭ 17 (-81.52%)
Mutual labels:  jvm
graalvm
A Cloud Native Buildpack that provides the GraalVM implementations of JREs and JDKs
Stars: ✭ 21 (-77.17%)
Mutual labels:  jvm
wasm.cljc
Spec compliant WebAssembly compiler, decompiler, and generator
Stars: ✭ 178 (+93.48%)
Mutual labels:  jvm
probes-api
Software Activity Metering - Probes Open API
Stars: ✭ 31 (-66.3%)
Mutual labels:  jvm
arquillian-graphene
Robust Functional Tests leveraging WebDriver with flavour of neat AJAX-ready API
Stars: ✭ 91 (-1.09%)
Mutual labels:  jvm
JavaResolver
Java class file inspection library for .NET.
Stars: ✭ 39 (-57.61%)
Mutual labels:  jvm
flamegrapher
Web frontend and REST API for Java Flight Recorder with Flamegraphs 🔥
Stars: ✭ 77 (-16.3%)
Mutual labels:  jvm
Algorithm-Math
算法 & 数学知识 & 重拾基础知识系列文章编写和收集
Stars: ✭ 19 (-79.35%)
Mutual labels:  jvm
micrometer-jvm-extras
A set of additional JVM process metrics for micrometer.io.
Stars: ✭ 113 (+22.83%)
Mutual labels:  jvm

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.

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

  • 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)
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].