简体   繁体   English

如何在没有JVM的情况下运行java程序?

[英]How to run java program without JVM?

我有一个简单的 java 程序,它只会打印 Hello World。是否可以在不使用安装在机器上的 JVM 的情况下进行打印?但是编译器在那里。

You can compile Java Byte Code to Machine Code using something like this: http://en.wikipedia.org/wiki/GNU_Compiler_for_Java您可以使用以下内容将 Java 字节代码编译为机器代码: http : //en.wikipedia.org/wiki/GNU_Compiler_for_Java

Or you can use any of the MANY Java to C/C++ converters out there to make a C/C++ version, and compile that.或者您可以使用许多 Java 到 C/C++ 转换器中的任何一个来制作 C/C++ 版本,然后编译它。 (Search Google for "Java C Converter") (在 Google 上搜索“Java C 转换器”)

Warning: I have never tried any of these, including the linked GNU Compiler, so I cannot make any attestations to their usefulness or quality.警告:我从未尝试过任何这些,包括链接的 GNU 编译器,所以我无法证明它们的有用性或质量。

@SplinterReality already pointed you to some compilers (googling "java bytecode to native code compiler" will show you some more options). @SplinterReality 已经向您指出了一些编译器(谷歌搜索“java 字节码到本机代码编译器”将向您展示更多选项)。

I will just expand on that seeing some people are a bit confused:我将扩展一下,看到有些人有点困惑:

The JVM is a program that takes java bytecode , which you get by running javac on your java source code (or you generate it in some other fashion). JVM 是一个采用 java字节码的程序,您可以通过在 java源代码上运行 javac 来获得它(或者您以其他方式生成它)。 Bytecode is just a set of instructions that the JVM understands, it's there to give you a consistent set of opcodes which are platform independent.字节码只是 JVM 理解的一组指令,它可以为您提供一组与平台无关的一致操作码。 It's the JVM's job to then map those opcodes to native instructions, that's why JVMs are platform dependent.然后将这些操作码映射到本机指令是 JVM 的工作,这就是 JVM 依赖于平台的原因。 That's why when you compile your java source code to java bytecode you can run it on every platform.这就是为什么当您将 Java 源代码编译为 Java 字节码时,您可以在每个平台上运行它。

That's why, whether you have java source or bytecode, you can take it and just compile it directly to native code (platform dependent, actually sometimes that's exactly what the JVM, and to be more precise the JIT, does - it compiles stuff to native code when it sees the need to).这就是为什么,无论您有 Java 源代码还是字节码,您都可以将其直接编译为本机代码(取决于平台,实际上有时这正是 JVM,更准确地说是 JIT 所做的 - 它将东西编译为本机代码)当它认为需要时编写代码)。 Still there's more to the JVM than just bytecode interpretation - for instance you need to take care of garbage collection. JVM 还不仅仅是字节码解释——例如,您需要处理垃圾收集。

So the answer is: yes, but do you really want to do it?所以答案是:是的,你真的想做吗? Unless you want to be running it on some embedded systems I don't really see any reason to.除非您想在某些嵌入式系统上运行它,否则我真的看不出有任何理由这样做。

Yees, it is possible to run a java program without a JVM, albeit with limitations.是的,虽然有限制,但可以在没有 JVM 的情况下运行 Java 程序。 Aside from the http://en.wikipedia.org/wiki/GNU_Compiler_for_Java , there is the GraalVM native-image generator, which could be used : https://www.graalvm.org .除了http://en.wikipedia.org/wiki/GNU_Compiler_for_Java之外,还有 GraalVM 本机图像生成器,可以使用: https ://www.graalvm.org。

If that program is a one file source code (not compiled in bytecode) then you can ;) Here or here .如果该程序是单文件源代码(未以字节码编译),那么您可以 ;)这里这里 Here will be fine as well ;) 这里也很好;)

Just put it there and press compile and run .把它放在那里,然后按compile and run

But it will work only with simple stuff only, and you have to have source code.但它只适用于简单的东西,而且你必须有源代码。

I would not be surprised if there would be a site that would allowed to upload class from users PC如果有一个网站允许从用户 PC 上传课程,我不会感到惊讶

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

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