Help us shape the future of Go! Take the 2026 Go Developer Survey

jnigi

package module
v0.0.0-...-a334487 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2018 License: BSD-2-Clause Imports: 7 Imported by: 0

README

jnigi

Java Native Interface Golang Interface.

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

Notes

Signals

The JVM calls sigaction, Golang requires that SA_ONSTACK flag be passed. Without this Golang will not be able to print the exception information eg. type, stack trace, line number. On Linux a solution is using LD_PRELOAD with a library that intercepts sigaction and adds the flag. (Code that does this: https://gist.github.com/timob/5d3032b54ed6ba2dc6de34b245c556c7)

Status

  • Documentation needed.
  • Tests for main functions tests are present.
  • Has been used in Golang (using Go 1.6) executable multi threaded applications on Linux / Windows.

Changes

  • 2016-08-01 Initial version.

Example

package main

import (
	"fmt"
	"github.com/timob/jnigi"
	"log"
)

func main() {
	_, env, err := jnigi.CreateJVM(jnigi.NewJVMInitArgs(false, true, jnigi.DEFAULT_VERSION, []string{"-Xcheck:jni"}))
	if err != nil {
		log.Fatal(err)
	}
    obj, err := env.NewObject("java/lang/Object")
    if err != nil {
    	log.Fatal(err)
    }
    v, err := obj.CallMethod(env, "hashCode", jnigi.Int)
    if err != nil {
    	log.Fatal(err)
    }
	fmt.Printf("object hash code: %d\n", v.(int))
}

Documentation

Index

Constants

View Source
const (
	JNI_VERSION_1_2 = C.JNI_VERSION_1_2
	JNI_VERSION_1_4 = C.JNI_VERSION_1_4
	JNI_VERSION_1_6 = C.JNI_VERSION_1_6
	DEFAULT_VERSION = JNI_VERSION_1_6
)
View Source
const (
	Void = Type(1 << iota)
	Boolean
	Byte
	Char
	Short
	Int
	Long
	Float
	Double
	Object
	Array
)

Variables

This section is empty.

Functions

func CreateJVM

func CreateJVM(jvmInitArgs *JVMInitArgs) (*JVM, *Env, error)

Types

type ByteArray

type ByteArray struct {
	// contains filtered or unexported fields
}

func (*ByteArray) CopyBytes

func (b *ByteArray) CopyBytes(env *Env) []byte

func (*ByteArray) GetCritical

func (b *ByteArray) GetCritical(env *Env) []byte

func (*ByteArray) GetObject

func (b *ByteArray) GetObject() *ObjectRef

returns jlo

func (*ByteArray) ReleaseCritical

func (b *ByteArray) ReleaseCritical(env *Env, bytes []byte)

func (*ByteArray) SetObject

func (b *ByteArray) SetObject(o *ObjectRef)

type Env

type Env struct {
	// contains filtered or unexported fields
}

func WrapEnv

func WrapEnv(envPtr unsafe.Pointer) *Env

func (*Env) CallStaticMethod

func (j *Env) CallStaticMethod(className string, methodName string, returnType interface{}, args ...interface{}) (interface{}, error)

func (*Env) DeleteGlobalRef

func (j *Env) DeleteGlobalRef(o *ObjectRef)

func (*Env) DeleteLocalRef

func (j *Env) DeleteLocalRef(o *ObjectRef)

func (*Env) FromObjectArray

func (j *Env) FromObjectArray(objRef *ObjectRef) []*ObjectRef

func (*Env) GetStaticField

func (j *Env) GetStaticField(className string, fieldName string, fieldType interface{}) (interface{}, error)

func (*Env) GetUTF8String

func (j *Env) GetUTF8String() *ObjectRef

return global reference to java/lang/String containing "UTF-8"

func (*Env) NewByteArray

func (j *Env) NewByteArray(n int) *ByteArray

func (*Env) NewByteArrayFromObject

func (j *Env) NewByteArrayFromObject(o *ObjectRef) *ByteArray

func (*Env) NewByteArrayFromSlice

func (j *Env) NewByteArrayFromSlice(src []byte) *ByteArray

func (*Env) NewGlobalRef

func (j *Env) NewGlobalRef(o *ObjectRef) *ObjectRef

func (*Env) NewObject

func (j *Env) NewObject(className string, args ...interface{}) (*ObjectRef, error)

func (*Env) NoReturnConvert

func (j *Env) NoReturnConvert()

func (*Env) PrecalculateSignature

func (j *Env) PrecalculateSignature(sig string)

func (*Env) RegisterNative

func (j *Env) RegisterNative(className, methodName string, returnType interface{}, params []interface{}, fptr interface{}) error

func (*Env) SetStaticField

func (j *Env) SetStaticField(className string, fieldName string, value interface{}) error

func (*Env) ToObjectArray

func (j *Env) ToObjectArray(objRefs []*ObjectRef, className string) (arrayRef *ObjectRef)

type JVM

type JVM struct {
	// contains filtered or unexported fields
}

func (*JVM) AttachCurrentThread

func (j *JVM) AttachCurrentThread() *Env

func (*JVM) DetachCurrentThread

func (j *JVM) DetachCurrentThread() error

type JVMInitArgs

type JVMInitArgs struct {
	// contains filtered or unexported fields
}

func NewJVMInitArgs

func NewJVMInitArgs(ignoreUnrecognizedArgs bool, includeDefaultArgs bool, version int, args []string) *JVMInitArgs

type ObjectArrayType

type ObjectArrayType string

type ObjectRef

type ObjectRef struct {
	// contains filtered or unexported fields
}

func WrapJObject

func WrapJObject(jobj uintptr, className string, isArray bool) *ObjectRef

func (*ObjectRef) CallMethod

func (o *ObjectRef) CallMethod(env *Env, methodName string, returnType interface{}, args ...interface{}) (interface{}, error)

func (*ObjectRef) Cast

func (o *ObjectRef) Cast(className string) *ObjectRef

func (*ObjectRef) GetField

func (o *ObjectRef) GetField(env *Env, fieldName string, fieldType interface{}) (interface{}, error)

func (*ObjectRef) IsInstanceOf

func (o *ObjectRef) IsInstanceOf(env *Env, className string) (bool, error)

func (*ObjectRef) IsNil

func (o *ObjectRef) IsNil() bool

func (*ObjectRef) JObject

func (o *ObjectRef) JObject() jobject

func (*ObjectRef) SetField

func (o *ObjectRef) SetField(env *Env, fieldName string, value interface{}) error

type ObjectType

type ObjectType string

type Type

type Type uint32

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL