简体   繁体   中英

Java call C library using native-image from GraalVM

I have a simple project: https://github.com/MarcoLunar/native-pid-test All what it is doing is calling getpid from C library. Project is as simple as:

public static void main(String[] args) throws Exception {
    System.out.println("start");
    C_lib cLib = Native.loadLibrary("c", C_lib.class);
    int getpid = cLib.getpid();
    System.out.println("pid = " + getpid);
    System.out.println("end");
  }

When launching from IDE everything works fine:

start
pid = 155080
end

When trying to build using native-image from GraalVM I got this error:

[simpletest:155323]      compile:   2,597.81 ms,  2.05 GB
Fatal error:org.graalvm.compiler.graph.GraalGraphError: java.lang.NullPointerException
        at node: 43|&
        at method: Object com.oracle.svm.reflect.JNIGeneratedMethodSupport_getFieldOffsetFromId_5041c78d77a7b3d62103393b72fc35d80d2cc709.invoke(Object, Object[])
        at org.graalvm.compiler.phases.common.CanonicalizerPhase$Instance.tryCanonicalize(CanonicalizerPhase.java:397)
        at org.graalvm.compiler.phases.common.CanonicalizerPhase$Instance.processNode(CanonicalizerPhase.java:325)
        at org.graalvm.compiler.phases.common.CanonicalizerPhase$Instance.processWorkSet(CanonicalizerPhase.java:302)
        at org.graalvm.compiler.phases.common.CanonicalizerPhase$Instance.run(CanonicalizerPhase.java:264)
        at org.graalvm.compiler.phases.common.CanonicalizerPhase.run(CanonicalizerPhase.java:177)
        at org.graalvm.compiler.phases.common.CanonicalizerPhase.run(CanonicalizerPhase.java:73)
        at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:214)
        at org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:147)
        at com.oracle.svm.hosted.code.CompileQueue.doInlineTrivial(CompileQueue.java:587)
        at com.oracle.svm.hosted.code.CompileQueue.access$000(CompileQueue.java:156)
        at com.oracle.svm.hosted.code.CompileQueue$TrivialInlineTask.run(CompileQueue.java:284)
        at com.oracle.graal.pointsto.util.CompletionExecutor.lambda$execute$0(CompletionExecutor.java:173)
        at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
        at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
        at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
Caused by: java.lang.NullPointerException
        at org.graalvm.compiler.nodes.calc.BinaryArithmeticNode.tryConstantFold(BinaryArithmeticNode.java:120)

I have tried many times, many different solutions... but for now I have no more to check. I am asking for help in repairing the project. I think this is possible, because with some configurations I have the same error as in https: //github.com/oracle/graal/issues/2261... where someone fixed it... but didn't write the solution: (

I think currently JNA doesn't work in native image. You can use JNI if that's possible. Or you can use another interface to native code that will work specifically in the native images. Some info can be found in the javadoc of org.graalvm.nativeimage.c and its subpackages: https://www.graalvm.org/sdk/javadoc/index.html?org/graalvm/nativeimage/c/package-summary.html Here's an example of using it: https://www.praj.in/posts/2020/opengl-demo-using-graalvm/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM