timob / jnigi

Golang Java JNI library
BSD 2-Clause "Simplified" License
166 stars 44 forks source link

How to run the main method in java? #87

Open andy-hsiung opened 3 months ago

andy-hsiung commented 3 months ago

java code: public class GoSdkApplication {

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

} go code: package main

import ( "log" "runtime"

"github.com/timob/jnigi"

)

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{"-Djava.class.path=/Users/andy/gosdk.jar"}))

obj, err := env.NewObject("com/weixun/openapi/GoSdkApplication")
if err != nil {
    log.Fatal(err)
}
err = obj.CallMethod(env, "main", jnigi.Object, "plainText")
if err != nil {
    log.Fatal(err)
}

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

} ,The following error isException in thread "main" java.lang.NoClassDefFoundError: com/weixun/openapi/GoSdkApplication Caused by: java.lang.ClassNotFoundException: com.weixun.openapi.GoSdkApplication at java.net.URLClassLoader.findClass(URLClassLoader.java:382) at java.lang.ClassLoader.loadClass(ClassLoader.java:418) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) at java.lang.ClassLoader.loadClass(ClassLoader.java:351) 2024/08/09 16:04:56 Java exception occurred. check stderr/logcat exit status 1,Please help me with this problem, much appreciated