简体   繁体   English

Java 使用来自 GraalVM 的本机图像调用 C 库

[英]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.我有一个简单的项目: https://github.com/MarcoLunar/native-pid-test它所做的就是从 C 库中调用 getpid。 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:从 IDE 启动时,一切正常:

start
pid = 155080
end

When trying to build using native-image from GraalVM I got this error:当尝试使用来自 GraalVM 的本机映像进行构建时,出现此错误:

[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: (我认为这是可能的,因为在某些配置下,我遇到了与 https 中相同的错误://github.com/oracle/graal/issues/2261... 有人修复了它...但没有写解决方案: (

I think currently JNA doesn't work in native image.我认为目前 JNA 不适用于本机图像。 You can use JNI if that's possible.如果可能的话,您可以使用 JNI。 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/ 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下面是一个使用它的例子: https://www.praj.in/posts/2020/opengl-demo-using-graalvm/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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