我可以将 Java 编译为本机代码吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2991799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I compile Java to native code?
提问by barroco
Is there any way to compile from Java to standalone (or library) machine code without requiring a JVM?
有什么方法可以在不需要 JVM 的情况下从 Java 编译为独立(或库)机器代码?
回答by James Kingsbery
There used to be a tool called GCJ that was part of GCC, but it's been removed. Now, all the links in the GCC site re-direct to their non-GCJ equivalents.
曾经有一个名为 GCJ 的工具是 GCC 的一部分,但它已被删除。现在,GCC 站点中的所有链接都重定向到其非 GCJ 等效项。
NB: the comments all refered to my original answer saying you can compile Java to native code with GCJ.
注意:所有评论都参考了我的原始答案,说您可以使用 GCJ 将 Java 编译为本机代码。
回答by Peter Lawrey
Yes, the JIT in the JVM does exactly that for you.
是的,JVM 中的 JIT 正是为您做到了这一点。
In fact it can produce faster code than compiling the code in advance as it can generate code optimised for the specific platform based on how the code is used at runtime.
事实上,它可以生成比预先编译代码更快的代码,因为它可以根据代码在运行时的使用方式生成针对特定平台优化的代码。
The JVM is always involved even if a very high percentage is compiled to native code as you could load and run byte code dynamically.
即使非常高的百分比被编译为本机代码,也始终涉及 JVM,因为您可以动态加载和运行字节代码。
回答by Mark
Excelsior JETis a commercial Java to native code compiler. However, it was discontinued in May 2019.
Excelsior JET是一种商业 Java 到本机代码的编译器。但是,它已于 2019 年 5 月停产。
回答by RobAu
Yes!
是的!
Oracle has been working on the GraalVm, which supports Native Images. Check here: https://www.graalvm.org/
Oracle 一直致力于支持原生图像的 GraalVm。在这里查看:https: //www.graalvm.org/
Native ImageThe native image feature with the GraalVM SDK helps improve the startup time of Java applications and gives them a smaller footprint. Effectively, it's converting bytecode that runs on the JVM (on any platform) to native code for a specific OS/platform — which is where the performance comes from. It's using aggressive ahead-of-time (AOT) optimizations to achieve good performance.
本机映像与GraalVM SDK原生图像特征有助于提高Java应用程序的启动时间,让他们有一个更小的空间。实际上,它将在 JVM(在任何平台上)运行的字节码转换为特定操作系统/平台的本机代码——这就是性能的来源。它使用积极的提前 (AOT) 优化来实现良好的性能。
See more:
查看更多:
Summary
https://www.graalvm.org/docs/getting-started/#native-imagesDemos: Native images for faster startup
https://www.graalvm.org/docs/examples/native-list-dir/Detailed: 'Ahead-of-time Compilation'
https://www.graalvm.org/docs/reference-manual/aot-compilation/