我可以将 Java 编译为本机代码吗?
有没有什么方法可以在不需要 JVM 的情况下从 Java 编译为独立(或库)机器代码?
Is there any way to compile from Java to standalone (or library) machine code without requiring a JVM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
曾经有一个名为 GCJ 的工具是 GCC 的一部分,但它已被删除。现在,GCC 站点中的所有链接都重新定向到其非 GCJ 等效链接。
注意:这些评论都引用了我原来的回答,说你可以使用 GCJ 将 Java 编译为本机代码。
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.
NB: the comments all refered to my original answer saying you can compile Java to native code with GCJ.
是的!
Oracle 一直在开发支持 Native Images 的 GraalVm。检查此处:https://www.graalvm.org/
查看更多:
摘要
https://www.graalvm.org/docs/getting-started/#原生图像
演示:用于加快启动速度的本机映像
https://www.graalvm.org/docs/examples/native-list -dir/
详细:“提前编译”
https://www.graalvm.org/docs/reference-manual/aot -编译/
Micronaut 平台使用 GraalVM 制作原生微服务:
简单示例
编译 java:
编译为本机:
运行(仅需要可执行文件):
输出
Yes!
Oracle has been working on the GraalVm, which supports Native Images. Check here: https://www.graalvm.org/
See more:
Summary
https://www.graalvm.org/docs/getting-started/#native-images
Demos: 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/
The Micronaut platform uses GraalVM to make native microservices:
Simple example
Compiling java:
Compiling to native:
Running (only the executable is needed):
Output
Excelsior JET 是一个商业 Java 到本机代码的编译器。但该产品已于 2019 年 5 月停产。
Excelsior JET is a commercial Java to native code compiler. However, it was discontinued in May 2019.
是的,JVM 中的 JIT 正是为您做的。
事实上,它可以比提前编译代码生成更快的代码,因为它可以根据代码在运行时的使用方式生成针对特定平台优化的代码。
即使很大一部分被编译为本机代码,JVM 也始终会参与其中,因为您可以动态加载和运行字节代码。
Yes, the JIT in the JVM does exactly that for you.
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.
另一种可能性是
RoboVM
。但是,它似乎仅适用于
Linux
、iOS
和Mac OS X
。截至今天,该项目似乎仍然存在,与网上的一些帖子相反 声称该项目已经死亡。
Another possibility would be
RoboVM
.However, it only seems to work on
Linux
,iOS
andMac OS X
.As of today, the project still seems somewhat alive contrary to some posts online claiming the project to be dead.