简体   繁体   English

编译器在运行时之前将Java字节码转换为与平台无关的C代码?

[英]Compiler to translate Java bytecode to platform-independent C code before runtime?

I'm looking for a compiler to translate Java bytecode to platform-independent C code before runtime (Ahead-of-Time compilation). 我正在寻找一个编译器,在运行时(Ahead-of-Time编译)之前将Java字节码转换为与平台无关的C代码。

I should then be able to use a standard C compiler to compile the C code into an executable for the target platform. 然后,我应该能够使用标准C编译器将C代码编译为目标平台的可执行文件。 I understand this approach is suitable only for certain Java applications that are modified infrequently. 我知道这种方法仅适用于不经常修改的某些Java应用程序。

So what Java-to-C compilers are available? 那么Java-to-C编译器可用?

I could suggest a tool called JCGO which is a Java source to C translator. 我可以建议一个名为JCGO的工具,它是C语言翻译器的Java源代码。 If you need to convert bytecode then you can decompile the class files by some tool (eg, JadRetro+Jad) and pass the source files to JCGO. 如果需要转换字节码,则可以通过某种工具(例如,JadRetro + Jad)对类文件进行反编译,并将源文件传递给JCGO。 The tool translates all the classes of your java program at once and produces C files (one .c and .h for each class), which could, further, be compiled (by third-party tools) into highly-optimized native code for the target platform. 该工具一次翻译java程序的所有类,并生成C文件(每个类一个.c和.h),可以进一步编译(通过第三方工具)为高度优化的本机代码。目标平台。 Java generics is not supported yet. Java泛型尚不支持。 AWT/Swing and SWT are supported. 支持AWT / Swing和SWT。

Why do that? 为什么这样? The Java virtual machine includes a runtime Java-to-assembly compiler. Java虚拟机包括运行时Java到程序集编译器。

Compilation at runtime can yield better performance, since all information about runtime values is available. 运行时编译可以产生更好的性能,因为有关运行时值的所有信息都可用。 While ahead-of-time compilation has to take assumptions about runtime values and thus may emits less fast code. 虽然提前编译必须对运行时值进行假设,因此可能会发出较少的代码。 Please refer to Java vs C performance by Cliff Click for more details. 有关详细信息,请参阅Cliff Click的Java vs C性能

GCJ has this capability, but it hasn't got great support for Java features past 1.4, and Swing support is likely to be troublesome. GCJ具有这种功能,但它对1.4版本的Java功能没有很大的支持,而Swing的支持很可能很麻烦。 In practice though, the HotSpot JIT compiler beats all the ahead-of-time compilers for Java. 但实际上,HotSpot JIT编译器胜过Java的所有先期编译器。 See benchmarks from Excelsior JET. 请参阅Excelsior JET的基准测试 To clarify: GCJ converts java source/bytecode to natively compiled code 澄清一下:GCJ将java源/字节码转换为本机编译的代码

Toba will convert (old) Java bytecode to C source. Toba将(旧)Java字节码转换为C源。 However, it hasn't been updated since Java 1.1. 但是,自Java 1.1以来它还没有更新。 It may be helpful to partially facilitate the porting, but it just can't handle all the complex libraries Java has. 部分方便移植可能会有所帮助,但它无法处理Java所拥有的所有复杂库。

(I've been googling for this stuff, this is how I came to this question at SO.) (我一直在谷歌搜索这些东西,这就是我在SO处提出的这个问题。)

AFAIK, there is no such product but you have two options: AFAIK,没有这样的产品,但你有两个选择:

  • Implement your own byte-code to C transpiler. 将您自己的字节码实现到C语言转换器。 Byte-code is pretty simple, this isn't too hard. 字节码非常简单,这不是太难。

  • If you just want a native binary (ie when you don't need the C source code), then give GCJ a try. 如果您只想要一个原生二进制文件(即当您不需要C源代码时),那么请尝试GCJ

Note: If you're doing this for performance reasons, then you're going to be disappointed. 注意:如果出于性能原因这样做,那么你会感到很失望。 Java is generally as fast as C/C++. Java通常和C / C ++一样快。 Moreover, improvements to the VM will make all Java code faster but not your native binary. 此外,对VM的改进将使所有Java代码更快,但不是您的本机二进制代码。 Compiling the code will just give you a little better startup time. 编译代码只会给你一个更好的启动时间。

Not really an answer to my own question, but how does Oracle do it? 这不是我自己问题的答案,但Oracle是如何做到的?

http://download.oracle.com/docs/cd/B28359_01/java.111/b31225/chone.htm#BABCIHGA http://download.oracle.com/docs/cd/B28359_01/java.111/b31225/chone.htm#BABCIHGA

There used to be a product called TowerJ, which was essentially a "via C" static compiler for Java, but it is long gone. 曾经有一种名为TowerJ的产品,它本质上是Java的“via C”静态编译器,但它早已不复存在。

I was told that Sun Labs has created something like this as part of the Sun SPOT project, but I am not sure if it is public. 有人告诉我,Sun Labs已经创建了这样的东西,作为Sun SPOT项目的一部分,但我不确定它是否公开。

@BobMcGee: In the benchmarks you refer to, GCJ indeed loses, but Excelsior JET (which is a 32-bit AOT compiler) beats the 32-bit HotSpot on all three test systems, so I am not sure what was your point. @BobMcGee:在你提到的基准测试中,GCJ确实输了,但Excelsior JET(32位AOT编译器)在所有三个测试系统上击败了32位HotSpot ,所以我不确定你的观点是什么。

But, after all, there are lies, damn lies, and benchmarks . 但是,毕竟有谎言,该死的谎言和基准 :) :)

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

相关问题 与平台无关的Java &lt; - &gt; C#互操作性 - Platform-Independent Java <-> C# Interoperability 使用Java本机代码或C ++(QT,WxWidgets等)进行平台无关的编程 - Platform-independent programmming with Java native code or C++ (QT, WxWidgets etc.) Java中与平台无关的信任库路径 - Platform-independent trust store path in Java 从与开源平台无关的 Java 代码替换 Windows *.exe 中的图标 - Replacing icon in Windows *.exe from open-source platform-independent Java code 当 Java 需要 JVM 运行时,它如何独立于平台? - How is Java platform-independent when it needs a JVM to run? Java的RNG(使用种子)是否与平台无关? - Is Java's RNG (using seeds) platform-independent? Java中获取登录用户的可靠且与平台无关的方式 - reliable and platform-independent way of getting logged-in user in java Java的平台无关/ dev / null输出接收器 - platform-independent /dev/null output sink for Java 为什么在生成机器代码之前,JIT编译器需要将字节码转换为另一个中间表示形式 - Why do JIT Compiler need to translate bytecode to another intermediate representation before generating machine code 在 PATH 上启动程序的独立于平台的方式? - Platform-independent way to start program on PATH?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM