JAR 可执行文件的优缺点

发布于 2024-12-17 10:10:08 字数 148 浏览 2 评论 0原文

我在学校的 Java 科目中一直使用 JAR 文件导出项目。我注意到它的可移植性(假设使用的计算机安装了 Java)。然而,既然如此,为什么我还没有看到开发人员使用 JAR 文件分发 Java 程序呢?使用 JAR 可执行文件的优点(除了可移植性)和缺点(除了使用 C++)是什么?

I have been using JAR files to export my projects in my Java subject at school. I noticed it's portability (assuming the computer in use has Java installed). However, with that fact, why haven't I seen developers distribute Java programs using a JAR file? What are the pros (besides portability) and cons (aside from using C++) of using the JAR executable?

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

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

发布评论

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

评论(2

怼怹恏 2024-12-24 10:10:08

这个问题有两个组成部分。第一种是发送 JAR 且不使用安装程序。问题的第二个组成部分是部署可执行文件后在计算机上仅使用 JAR 的优缺点。我开始怀疑OP正在询问第一个组件。我的回答试图回答第二个部分。

优点

  1. 工作量少。不必为您想要支持的每个平台制作可执行文件。

缺点

  1. 如果用户没有安装 java,它无法告诉用户为什么它无法运行。 exe还可以为用户下载java。
  2. 如果您的应用程序需要特定版本的 Java,则它可能会使用错误的版本运行。本机可执行文件可以找到正确的可执行文件并使用它。由于 Java 6 已经存在这么久了,这一点不再像以前那么重要了。
  3. Windows 上的可执行文件无法有自定义图标。
  4. 您无法控制启动选项,例如应用程序可以使用的最大内存。对于大多数应用程序来说这是可以的,但有时您需要更多内存。
  5. JAR 不能用于 Windows 服务。它周围必须有一个 exe 包装器(jsmooth 有这个)。

生成可执行文件的另一种替代方法是使用 JNLP。网页可以在将用户转发到 .jnlp 文件之前检查 java。

There are two components to the question. The first is shipping a JAR and using no installer. The second component to the question is the pros and cons of a using just a JAR on the machine once the executable has been deployed. I'm starting to suspect that the OP was asking about the first component. My answer tries to answer the second component.

Pros

  1. Less work. Don't have to make an executable for each platform you want to support.

Cons

  1. If the user doesn't have java installed it can't tell the user why it can't run. An exe can also download java for the user.
  2. If your app requires a specific version of Java it may run with the wrong version. A a native executable can locate the correct one and use it. This point isn't as important as it used to be since Java 6 has been around so long.
  3. Can't have an customized icon for the executable on Windows.
  4. You can't control startup options such as the maximum memory that your app can use. For most apps this is ok but sometimes you need more memory.
  5. A JAR cant be used for a Windows service. There must be an exe wrapper around it (jsmooth has this).

Another alternative to producing an executable is to use JNLP. A web page can check for java before it forwards the user to the .jnlp file.

静水深流 2024-12-24 10:10:08

我希望优势非常明显。我遇到的最大缺点是 JAR 中未包含所需的其他依赖项。可以包含 MANIFEST 来设置类路径 - 但这要求所有依赖项都以预定义名称存在于预定义位置。这可以通过加载脚本(设置类路径等)来解决 - 此时不再将所有内容都包含在单个 JAR 中,并且大多数优点都会丢失。

I'm hoping that the advantages are pretty obvious. The biggest disadvantage I've run into is when there are additional dependencies that are required that aren't included in the JAR. A MANIFEST can be included to set the classpath - but this requires that all of the dependencies exist with pre-defined names in a pre-defined location. This can be worked-around with a loader script (to set the classpath, etc.) - at which point not everything is contained in the single JAR anymore, and most of the advantages are lost.

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