Java程序和JVM生成的exe

发布于 2024-12-10 08:39:24 字数 343 浏览 0 评论 0原文

Java 是平台无关的,因为它使用依赖于平台的 JVM 来启动 Java 程序。 JVM 理解字节码并执行程序。我知道这样做的旧方法是解释器。但现在 JVM 正在使用 JIT。但我并没有清楚地理解JIT的概念。我认为,JVM 可以将字节码翻译为 exe(对于 Windows),之后我可以在没有 JVM 的情况下运行这个翻译后的程序。但我可以在 .net JIT 中看到生成的 exe,但在 Java 中看不到生成的 exe。

  1. 我该如何做到这一点(从 Java 创建本机 exe 文件)?
  2. 与相同的 C 应用程序相比,JVM 生成的 exe 的性能如何?
  3. Java如何处理静态链接和动态链接?

Java is platform independent , because it use a platform dependent JVM to launch a Java program. JVM understands bytes codes and executes program. I know old way of doing this was interpreter. But now JVM's is using JIT. But I didn't understand JIT concept clearly. I think, a jVM can translate byte codes to an exe (for Windows) and after this I can run this translated program without JVM. But I can see generated exe in .net JIT but I cannot see generated exe in Java.

  1. How can I do this (create a native exe file from Java)?
  2. What will be performance of JVM produced exe vs the same C application?
  3. How Java will handle static linking and dynamic linking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谁的新欢旧爱 2024-12-17 08:39:24

一些想法:

  1. 您不能仅使用核心 Java 生成 exe,并且 JVM 当然不会这样做。它仍然在 JVM 中运行字节码。这里运行的实际exe是java.exe。
  2. 很难说 exe 是否会明显更快,因为 JIT 生成的代码非常快。
  3. 如果您是这个意思,Java 不会创建 dll,但它可以通过 JNI 和 JNA 与格式良好的 dll(不是 .Net dll)进行交互。如果您想与 .Net 库交互,我认为您需要采用另一条路线,例如套接字或 COM 接口。

顺便说一句,确实存在从 java 类创建 exe 的程序,但大多数创建的文件仍然需要 JVM,而那些不需要 JVM 的好程序我相信都不是免费的。

Some thoughts:

  1. You can't generate an exe using just core Java, and the JVM certainly doesn't do this. It still runs the byte code in the JVM. The actual exe that runs here is java.exe.
  2. It's hard to say if an exe would be significantly faster since the JIT generates pretty darn fast code.
  3. Java doesn't create dll's if that's what you mean, but it can interact with well-formed dll's (not .Net dll's) via JNI and JNA. If you wanted to interact with .Net libraries, I think you would need to go another route such as sockets or COM interface.

Also as an aside, there do exists programs that create exe's from java classes, but most create files that still require a JVM, and the good ones that don't I believe are not free.

誰ツ都不明白 2024-12-17 08:39:24
  1. 这样做没有多大意义。
  2. 有时更快,有时更慢。
  3. 不知道。
  1. Does not make much sense to do so.
  2. Sometimes faster, sometimes slower.
  3. No idea.
[浮城] 2024-12-17 08:39:24

我认为,JVM 可以将字节码翻译为 exe(适用于 Windows),之后我可以在没有 JVM 的情况下运行这个翻译后的程序。

这是不正确的。

实际上,Hotspot JIT 编译器的工作原理是将各个方法编译为正在运行的 JRE 中的本机代码。他们通常只在调用几次方法来收集典型执行路径上的统计信息后才编译方法。 Hotspot JIT 编译器不会生成任何“exe”。

1) 我该如何做到这一点(从 Java 创建本机 exe 文件)?

有第三方应用程序可以执行此操作。但是,这样做会失去 JIT 编译的许多优点,例如针对当前程序运行的执行模式进行优化。

2) 与相同的 C 应用程序相比,JVM 生成的 exe 的性能如何?

这取决于应用程序。

3) Java 如何处理静态链接和动态链接?

Java 不处理这个。这取决于您用来创建可执行文件的第 3 方应用程序。


我建议不要走这条路。如果将代码作为“.exe”文件分发至关重要,那么您可能不应该使用 Java。

I think, a jVM can translate byte codes to an exe (for Windows) and after this I can run this translated program without JVM.

This is not correct.

In reality the Hotspot JIT compilers work by compiling individual methods to native code within the running JRE. They typically only bother to compile methods after they have been called a few times to gather stats on typical execution paths. No "exe" is ever produced by the Hotspot JIT compilers.

1) How can I do this (create a native exe file from Java)?

There are third party applications that will do this. However, by doing this you lose a lot of the advantages of JIT compilation, such as optimizing for the execution patterns of the current program run.

2) What will be performance of JVM produced exe vs the same C application?

That depends on the application.

3) How Java will handle static linking and dynamic linking?

Java doesn't handle this. It depends on the 3rd party application that you use to create the executable.


I would recommend not going down this path. If it is critical that you distribute your code as ".exe" files, you probably shouldn't be using Java.

情释 2024-12-17 08:39:24

我该如何做到这一点(从 Java 创建本机 exe 文件)?

人们普遍错误地认为这在某种程度上会更好。然而,我还没有看到什么好的情况。有些产品可以编译二进制文件,例如 Excelsior JET。 GCC 可以编译 Java 1.4 的二进制文件,但这不是当前的项目 AFAIK。

与相同的 C 应用程序相比,JVM 生成的 exe 的性能如何?

你无法比较两者。如果你想要一个面向对象的程序,你不能轻易地用 C 语言编写它,而且如果不运行它几乎肯定会慢得多。如果你想写一个C风格的程序,就用C语言来写。

Java 如何处理静态链接和动态链接?

JVM 执行动态后期链接。事实上,当您加载和卸载类加载器时,它可以多次链接和重新链接代码。

How can I do this (create a native exe file from Java)?

There is a common mis conception that this will be better in some way. However, I haven't seen a good situation where it is. There are products like Excelsior JET which can compile a binary. GCC can compile a binary for Java 1.4 but this is not a current project AFAIK.

What will be performance of JVM produced exe vs the same C application?

You can't compare the two. If you want an Object Orientated Program, you can't easily write this in C and it will almost certainly be much slower to write if not run. If you want to write a C style program, write it in C.

How Java will handle static linking and dynamic linking?

The JVM does dynamic late linking. If fact it can link and re-link code multiple times as you load and unload class loaders.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文