简体   繁体   English

我可以将 Java 编译为本机代码吗?

[英]Can I compile Java to native code?

有什么方法可以在不需要 JVM 的情况下从 Java 编译为独立(或库)机器代码?

There used to be a tool called GCJ that was part of GCC, but it's been removed .曾经有一个名为 GCJ 的工具是 GCC 的一部分,但它已被删除 Now, all the links in the GCC site re-direct to their non-GCJ equivalents.现在,GCC 站点中的所有链接都重定向到其非 GCJ 等效项。

NB: the comments all refered to my original answer saying you can compile Java to native code with GCJ.注意:评论都参考了我的原始答案,说您可以使用 GCJ 将 Java 编译为本机代码。

Yes!是的!

Oracle has been working on the GraalVm, which supports Native Images. Oracle 一直致力于支持原生图像的 GraalVm。 Check here: https://www.graalvm.org/在这里查看: https : //www.graalvm.org/

Native Image The native image feature with the GraalVM SDK helps improve the startup time of Java applications and gives them a smaller footprint.本机映像与GraalVM SDK原生图像特征有助于提高Java应用程序的启动时间,让他们有一个更小的空间。 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.实际上,它将在 JVM(在任何平台上)运行的字节码转换为特定操作系统/平台的本机代码——这就是性能的来源。 It's using aggressive ahead-of-time (AOT) optimizations to achieve good performance.它使用积极的提前 (AOT) 优化来实现良好的性能。

See more:查看更多:

Excelsior JET is a commercial Java to native code compiler. Excelsior JET是一种商业 Java 到本机代码的编译器。 However, it was discontinued in May 2019.但是,它已于 2019 年 5 月停产。

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,因为您可以动态加载和运行字节代码。

Another possibility would be RoboVM .另一种可能性是RoboVM However, it only seems to work on Linux , iOS and Mac OS X .然而,它似乎只适用于LinuxiOSMac OS X

As of today, the project still seems somewhat alive contrary to some posts online claiming the project to be dead .截至今天,该项目似乎仍然有些活跃,这与网上一些声称该项目已死的帖子相反。

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

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