简体   繁体   English

如何使用 Java/Eclipse 创建 Windows.exe(独立可执行文件)?

[英]How can I create a Windows .exe (standalone executable) using Java/Eclipse?

I'm new to the programming scene.我是编程领域的新手。 Been working with C++ for about 5 months now, and have decided I want to start getting into Java. I'm using Eclipse as my IDE, and obviously Java for the language.与 C++ 一起工作了大约 5 个月,并决定开始进入 Java。我使用 Eclipse 作为我的 IDE,显然 Java 用于语言。 I'm trying to write a simple HelloWorld application, which can be run through a command prompt executable.我正在尝试编写一个简单的 HelloWorld 应用程序,它可以通过命令提示符可执行文件运行。

In Visual Studio, it seems it's rather easy to create an executable.在 Visual Studio 中,创建可执行文件似乎相当容易。 All I've ever had to do is use a pull down arrow and choose Release, and then run my build.我所要做的就是使用下拉箭头并选择发布,然后运行我的构建。 The purpose of wanting to be able to write/run Java in command prompt is so that I'm able to practice some of the language basics before I go in full force with Swing.希望能够在命令提示符下编写/运行 Java 的目的是让我能够在 go 之前完全使用 Swing 练习一些语言基础知识。

So bottom line, what is the easiest way to create a command prompt.exe written with Java?所以归根结底,创建用 Java 编写的命令提示符.exe 的最简单方法是什么?

Thanks in advance!提前致谢!

Java doesn't natively allow building of an exe , that would defeat its purpose of being cross-platform. Java本身不允许构建exe ,这会破坏其跨平台的目的。

AFAIK, these are your options: AFAIK,这些是您的选择:

  1. Make a runnable JAR . 制作一个可运行的JAR If the system supports it and is configured appropriately, in a GUI, double clicking the JAR will launch the app. 如果系统支持并且配置正确,则在GUI中双击JAR将启动应用程序。 Another option would be to write a launcher shell script/batch file which will start your JAR with the appropriate parameters 另一种选择是编写一个启动程序shell脚本/批处理文件,它将使用适当的参数启动您的JAR

  2. There also executable wrappers - see How can I convert my Java program to an .exe file? 还有可执行包装器 - 请参阅如何将我的Java程序转换为.exe文件?

See also: Convert Java to EXE: Why, When, When Not and How 另请参阅: 将Java转换为EXE:为什么,何时,何时以及如何

Typical Java programs compile into .jar files, which can be executed like .exe files provided the target machine has Java installed and that Java is in its PATH. 典型的Java程序编译成.jar文件,如果目标机器安装了Java并且Java在其PATH中,则可以像.exe文件一样执行。 From Eclipse you use the Export menu item from the File menu. 从Eclipse中,您可以使用“文件”菜单中的“导出”菜单项。

Creating a native installer using jpackage使用jpackage创建本机安装程序

A java packaging tool named jpackage was released as part of the Java Development Kit (JDK) version 16. java 名为jpackage打包工具作为 Java 开发工具包 (JDK) 版本 16 的一部分发布。

This tool works in conjunction with native packaging tools for various platforms (eg WIX for Windows, RPM , and DEB for Linux distributions, DMG for Mac) to allow building native installers for Java applications which can then be run as executables.该工具与各种平台的本机打包工具(例如WIX用于 Windows, RPMDEB用于 Linux 发行版, DMG用于 Mac)结合使用,允许为 Java 应用程序构建本机安装程序,然后可以作为可执行文件运行。 For distribution, it may be possible to distribute just the executable for the application, independent of the installer (I don't know, I didn't try that).对于分发,可能只分发应用程序的可执行文件,独立于安装程序(我不知道,我没试过)。

A nice, tutorial style, blog post that describes the use of the jpackage tool to create a native Windows installer for a Java application is:一篇不错的教程风格的博客文章描述了如何使用jpackage工具为 Java 应用程序创建本机 Windows 安装程序:

Customizing the runtime image using jlink and jdeps使用jlinkjdeps自定义运行时映像

The packaging tool can (optionally) be combined with the jlink tool:打包工具可以(可选)与jlink工具结合使用:

jlink - assemble and optimize a set of modules and their dependencies into a custom runtime image jlink - 将一组模块及其依赖项组装并优化为自定义运行时映像

This allows you to customize the runtime image for your application to only include the required custom selected modular parts of your application code, java runtime, and 3rd party libraries, rather than distributing a complete java runtime.这允许您为您的应用程序自定义运行时映像,以仅包括您的应用程序代码、java 运行时和第三方库所需的自定义选定模块化部分,而不是分发完整的 java 运行时。

Optionally, you can also use the jdeps tool to determine inputs to jlink .或者,您也可以使用jdeps工具来确定jlink的输入。

Complete discussion of usage of jdeps + jlink + jpackage + a native bundle creator (eg wix/rpm/deb/dmg) is outside of scope for a StackOverflow answer, but various resources can be found on the web if you search.关于jdeps + jlink + jpackage + 本地包创建器(例如 wix/rpm/deb/dmg)用法的完整讨论不在 scope 的 StackOverflow 答案中,但如果您搜索,可以在 web 上找到各种资源。

Creating a standalone .exe instead of an installer创建独立的.exe而不是安装程序

This can be done using warp-packer to create a exe out of the image and app launcher created by jlink .这可以使用warp-packerjlink创建的图像和应用程序启动器中创建一个 exe 来完成。

Third party tools can help deliver a solution第三方工具可以帮助提供解决方案

If you wish to use all these tools in combination, things can get complicated, and I'd advise using a 3rd party utility or template to help perform this task.如果您希望结合使用所有这些工具,事情会变得复杂,我建议您使用第 3 方实用程序或模板来帮助执行此任务。 For example:例如:

  • badass-jlink-plugin ( documentation ) -> "allows you to create custom runtime images for modular applications with minimal effort. It also lets you create an application installer with the jpackage tool." badass-jlink-plugin文档)->“允许您以最小的努力为模块化应用程序创建自定义运行时图像。它还允许您使用jpackage工具创建应用程序安装程序。”

For JavaFX specific applications (as your question has a JavaFX tag), you could review:对于 JavaFX 特定应用程序(因为您的问题有 JavaFX 标签),您可以查看:

  • JPackageScriptFX -> "demonstrates how projects can use scripts to build self-contained, platform-specific executables and installers of their JavaFX applications via the jdeps, jlink, and jpackage tools." JPackageScriptFX -> “演示项目如何使用脚本通过 jdeps、jlink 和 jpackage 工具构建独立的、平台特定的可执行文件和其 JavaFX 应用程序的安装程序。”

For native mobile deployments, see Gluon Mobile对于本机移动部署,请参阅 Gluon Mobile

This question was Windows specific, but for completeness, if your target is native application deployment on a mobile device, then likely you will need to use a 3rd party solution such as Gluon Mobile .这个问题是 Windows 具体的,但为了完整起见,如果您的目标是在移动设备上部署本机应用程序,那么您可能需要使用第三方解决方案,例如Gluon Mobile


Background Rationale (ignore if not needed)背景原理(不需要的请忽略)

creating a native installer using jpackage might be a bit of work, so why would you do it?使用jpackage创建本机安装程序可能需要一些工作,那么您为什么要这样做呢?

Here are some reasons:以下是一些原因:

  1. When an application is distributed via an installer created using jpackage , that application can be installed on a target system without requiring the user to manually install other dependencies (such as a Java Runtime).当应用程序通过使用jpackage创建的安装程序分发时,该应用程序可以安装在目标系统上,而无需用户手动安装其他依赖项(例如 Java 运行时)。 The installer will take care of ensuring that your application and any dependencies it requires are installed.安装程序将负责确保安装您的应用程序及其所需的任何依赖项。
  2. The application ships with its own customized version of the Java Runtime.该应用程序附带其自己的定制版本 Java 运行时。 This means that if the user doesn't install a JRE, and, if they have installed a version of the JRE which is incompatible with your application, your application will still function correctly.这意味着如果用户没有安装 JRE,并且如果他们安装了与您的应用程序不兼容的 JRE 版本,您的应用程序仍将正确运行 function。
  3. The application can be installed and uninstalled using standard OS facilities for the target platform.可以使用目标平台的标准操作系统设施安装和卸载应用程序。 Most users of those platforms are familiar with these.这些平台的大多数用户都熟悉这些。

Yes, Java is cross-platform for the most part, but many users don't care much about that, instead they just want a smooth and familiar installation (and uninstallation) experience for their application and jpackage can help accomplish that.是的,Java 在大多数情况下是跨平台的,但许多用户对此并不关心,相反,他们只是希望他们的应用程序有一个流畅和熟悉的安装(和卸载)体验,而jpackage可以帮助实现这一点。

Creating .exe distributions isn't typical for Java. 创建.exe发行版对于Java来说并不常见。 While such wrappers do exist, the normal mode of operation is to create a .jar file. 虽然这样的包装器确实存在,但正常的操作模式是创建.jar文件。

To create a .jar file from a Java project in Eclipse, use file->export->java->Jar file. 要从Eclipse中的Java项目创建.jar文件,请使用file-> export-> java-> Jar文件。 This will create an archive with all your classes. 这将创建包含所有类的存档。

On the command prompt, use invocation like the following: 在命令提示符下,使用如下调用:

java -cp myapp.jar foo.bar.MyMainClass java -cp myapp.jar foo.bar.MyMainClass

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

相关问题 如何创建独立的Wicket可执行文件? - How can I create a standalone Wicket executable? 我可以使用 Inno Setup 为 Java 应用程序创建独立的 .exe 安装程序吗? - Can I create a standalone .exe installer for a Java application with Inno Setup? Windows的Java独立可执行文件 - Java Standalone executable for windows 如何在可执行文件(.exe)的 Netbeans 中导出我的 Java 项目? - How can I Export my Java Project in Netbeans in Executable(.exe)? 如何使用Eclipse在JAVA中创建可执行文件(双击启动程序的文件,如.exe)? - How to create an executable (a file that starts the program on double-click like an .exe) in JAVA with Eclipse? 在Eclipse中运行独立的Java可执行文件导致NoClassDefFound - running standalone java executable in eclipse results in NoClassDefFound 如何以可执行格式(Java,eclipse)导出我的程序? - How can I export my program in an executable format (Java, eclipse)? 使用Eclipse创建java可执行文件 - Create a java executable with Eclipse 如何从java代码创建* standalone * linux可执行文件 - How to create a *standalone* linux executable from java code 如何在 Java 中创建独立的 .exe(无需安装程序和 JRE 即可运行) - How to create a standalone .exe in Java (that runs without an installer and a JRE)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM