i want to run java from go,but it didnot work

-1

i want to run java from go. first, i try to run java from c, it work. bug it can't run from go.

(windows 10 64)

1

the java code

HelloWorld.java

public class HelloWorld{
    public static void sayhello(){
        System.out.println("Hello World from java !");
    }
}

2

> javac HelloWorld.java 

generate a file 'HelloWorld'

3

> javap -p -s HelloWorld

Compiled from "HelloWorld.java"
public class HelloWorld {
  public HelloWorld();
    descriptor: ()V

  public static void sayhello();
    descriptor: ()V
}

4

hello.c

#include <jni.h>
#include <string.h>
int main() 
{ 
    JavaVMOption options[1]; 
    JNIEnv *env; 
    JavaVM *jvm; 
    JavaVMInitArgs vm_args; 
    long status; 
    jclass cls; 
    jmethodID mid; 
    jint square; 
    jboolean not; 

    options[0].optionString = "-Djava.class.path=."; //setting classpath
    memset(&vm_args, 0, sizeof(vm_args)); 
    vm_args.version = JNI_VERSION_1_2; 
    vm_args.nOptions = 1; 
    vm_args.options = options; 
    status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); 

    if (status != JNI_ERR) 
    { 
        //cls = (*env)->FindClass(env, "java/lang/Object"); 
        cls=(*env)->FindClass(env,"HelloWorld");

        printf("find the class\n");
        if(cls !=0) 
        {
            //mid = (*env)->GetStaticMethodID(env, cls, "main", "(I)I"); 
            mid=(*env)->GetStaticMethodID(env,cls,"sayhello","()V");
            printf("find the method\n");
            if(mid !=0) 
            {
                printf("exec method\n");
                (*env)->CallStaticVoidMethod(env, cls, mid, NULL); //执行方法
            } 
        } 
        else{
            printf("not found\n");
        }
        (*jvm)->DestroyJavaVM(jvm); 
        return 0; 
    } 
    else 
        return -1; 
}

5

gcc -I"C:\Program Files"\Java\jdk1.8.0_251\include -I"C:\Program Files"\Java\jdk1.8.0_251\include\win32 hellow_world.c -L "C:\Program Files"\Java\jdk1.8.0_251\jre\bin\server -ljvm

generate a file 'a.exe'

6

run a.exe

> a.exe

find the class
find the method
exec method
Hello World from java !

why it didn't work in golang?

hello.go

package main
/*
#cgo CFLAGS: -I"C:/Program Files"/Java/jdk1.8.0_251/include -I"C:/Program Files"/Java/jdk1.8.0_251/include/win32
#cgo LDFLAGS:  -L"C:/Program Files"/Java/jdk1.8.0_251/jre/bin/server -ljvm
#include<stdio.h>
#include <jni.h>
#include <string.h>

int main1()
{
    JavaVMOption options[1];
    JNIEnv *env;
    JavaVM *jvm;
    JavaVMInitArgs vm_args;
    long status;
    jclass cls;
    jmethodID mid;
    jint square;
    jboolean not;

    options[0].optionString = "-Djava.class.path=."; //setting classpath
    memset(&vm_args, 0, sizeof(vm_args));
    vm_args.version = JNI_VERSION_1_2;
    vm_args.nOptions = 1;
    vm_args.options = options;
    status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

    if (status != JNI_ERR)
    {
        //cls = (*env)->FindClass(env, "java/lang/Object");
        cls=(*env)->FindClass(env,"HelloWorld");

        printf("find the class\n");
        if(cls !=0)
        {
            //mid = (*env)->GetStaticMethodID(env, cls, "main", "(I)I");
            mid=(*env)->GetStaticMethodID(env,cls,"sayhello","()V");
            printf("find the method\n");
            if(mid !=0)
            {
                printf("exec method\n");
                (*env)->CallStaticVoidMethod(env, cls, mid, NULL); 
            }
        }
        else{
            printf("not found\n");
        }
        (*jvm)->DestroyJavaVM(jvm);
        return 0;
    }
    else
        return -1;
}
*/
import "C"

func main() {
    C.main1()
}

enter image description here

Exception 0xc0000005 0x0 0x0 0x288103b6
PC=0x288103b6

runtime: unknown pc 0x288103b6
stack: frame={sp:0x83f538, fp:0x0} stack=[0x0,0x83fdf0)
000000000083f438:  0000000000000000  0000000000000000 
000000000083f448:  0000000000000000  0000000000000000 
000000000083f458:  0000000000000000  0000000000000000 
000000000083f468:  0000000000000000  cafebabecafebabe 
000000000083f478:  cafebabecafebabe  0000000000000001 
000000000083f488:  00000000000d0f00  00000000000d0cc0 
000000000083f498:  00007ff80000000d  00000000000f0000 
000000000083f4a8:  00007ff89828c822  0000000000000000 
000000000083f4b8:  0000000000000040  0000000000000100 
000000000083f4c8:  0000000000000000  0000000000000004 .....

how can i run it in golang? You might get inspiration from here: https://github.com/golang/mobile/blob/master/asset/asset_android.go

java
c
go
java-native-interface
jnienv
asked on Stack Overflow May 18, 2020 by Benson Chan

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0