我有一个JavaFX应用程序,我使用它没有模块(即我删除了模块信息文件).该应用程序正在得到建设与mvn clean package刚刚好,我可以运行它与java -jar myapp-with-dependecies.jar刚刚好.有一个Main类运行我的javafx应用程序(lifehack运行javafx应用程序没有模块,在互联网上找到):

public class Main {
  public static void main(String[] args) {
    DesktopApplication.main(args);
  }
}

以下是我在POM中的插件:

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.11.0</version>
        <configuration>
          <annotationProcessorPaths>
            <path>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>1.18.30</version>
            </path>
          </annotationProcessorPaths>
          <source>17</source>
          <target>17</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>my.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>my.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

I also found this configuration somewhere in the internet when searching how to make JavaFX run without modules. And obviously there are some files in my resources package, i.e. fxml files, css files and configuration file for hibernate.
Now you have a full understanding of structure of my project. Other than that, there are just dependencies for logic and my classes, that are not critical for my question, so you also have a reproducible example. These are my javafx dependencies:

<dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-controls</artifactId>
      <version>19.0.2</version>
    </dependency>
    <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-fxml</artifactId>
      <version>19.0.2</version>
    </dependency>

我的问题是:我现在如何制作我的应用程序的可执行.exe文件?我也想包括Java在它,这样客户将不必安装所需的Java版本.到目前为止,我try 的是:

  1. 我使用了Launch4j,它创建了一个.exe文件,但问题是它假定客户端已经安装了Java.
  2. 我从GitHub下载了一些项目,它已经配置为可以很容易地从JavaFX应用程序中创建包含Java的可执行文件.问题是我已经有了我的项目,我不想仅仅为了构建exe而将很多文件转移到新项目中.

I think there should be a more clear way of how to do it, using some commands or something like that. Unfortunately, I didn't find a good step by step explanation of how to do it. So this is my question, if someone will enlighten me and tell me how to do it, I will appreciate that, thanks.
UPD: Just to make things clear, I'm using Linux and want to create .exe file for Windows. Is that possible?

推荐答案

Preface:

对于标准的东西来说,这是不可能的.你必须靠自己创造一些东西.

我可以断定这是可能的,因为我做了同样的事情,但情况相反.我使用Windows,并在Docker-Ubuntu-Container中执行该命令.

Solution 1, Part 1:

要做到这一点,最好的方法是使用jpack.

以下是指向这些文档的直接链接:https://docs.oracle.com/en/java/javase/14/docs/specs/man/jpackage.html

JPackage是Oracle自己开发的工具,包含在所有较新的JDK中.

Additional information:

有时,创建Need jPackage命令有点棘手.

我编写了一个应用程序,可以为您创建此命令.之后,您可以使用打包的Open JDK 17或定制的可选JDK来执行此命令.或者,您也可以编辑、复制和执行它.

https://github.com/davidweber411/javafx-JavaFxProjectManager

你可以创建"不需要安装Java的便携exe应用程序"和一个安装程序,如果你想-创建桌面图标?创建Start ManüEntry?等.

Solution 1, Part 2:

在第1部分之后,您了解了"如何创建本机可执行文件"的机制.

JPackage依赖于操作系统.只能在Windows中创建EXE/MSI,在Linux中只能创建DEB,等等. 特别是JavaFX有额外的Linux和额外的Windows依赖.

您唯一的 Select 是在Linux下设置一个Windows VM并从该VM执行jPackage.您可以使用Docker设置Windows容器.

Solution 2 (Experimental):

您可以try 在Windows上安装JDK,将其复制到Linux系统中,然后使用Wine运行jpackage命令.也许下载Windows JDK并解压缩它就足够了.JPackage.exe位于JDK中.

Java相关问答推荐

如何在SystemiccationRetryListenerSupport中获得类级别的spring retryable annotation中指定的标签?

Java Stream,需要更新列表对象列表

在bash中将数组作为Java程序的参数传递

如何创建一个2d自上而下的移动系统,其中移动,同时持有两个关键是可能的处理?

我正在try 跟踪数组中最大的两个数字

如何解释Java中for-each循环中对Iterable的强制转换方法引用?

我如何知道MediaDiscoverer何时完成发现介质?

如何将Pane的图像快照保存为BMP?

try 在Android Studio中的infoWindow中使用EditText(Java)

从Spring6中的JPMS模块读取类时出现问题

如何使这两种方法合二为一?

深度优先搜索实现:算法只向右搜索

在Java泛型中使用通配符时,如何推断类型

Java HashMap保留所有时间复杂性

OAuth:登录后无法查看Google邮箱地址

如何在右击时 Select 新行?

Spring Boot Security-每个端点都被403禁止,Spring记录一个BasicErrorController#错误(HttpServlet请求)

try 添加;按流派搜索;在Web应用程序上,但没有;I don’我不知道;It’这个代码错了

@此处不能应用可为null的批注

如何组合Mono和Flux