首页 > 解决方案 > 如何使用 jar 文件为 linux 制作安装程序?

问题描述

问题

  1. 如果 linux 操作系统上没有安装 java jdk,jar 将不会运行。

  2. 如果在 linux 中运行 javafx jar 会出现这样的错误

    Error: Could not find or load main class application.Main Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

之后,我做到了。有效。

要使用终端运行 java-fx 应用程序,请执行以下步骤:

安装 openjfx(如果尚未安装):
sudo apt install openjfx

列出 javafx 库位置:
dpkg-query -L openjfx
输出应该是这样的:

. /usr/usr/share /usr/share/doc /usr/share/doc/openjfx /usr/share/doc/openjfx/TODO.Debian /usr/share/doc/openjfx/changelog.Debian.gz /usr/share /doc/openjfx/copyright /usr/share/openjfx /usr/share/openjfx/lib /usr/share/openjfx/lib/javafx.properties /usr/share/openjfx/lib/javafx.base.jar /usr/share /openjfx/lib/javafx.controls.jar /usr/share/openjfx/lib/javafx.fxml.jar /usr/share/openjfx/lib/javafx.graphics.jar /usr/share/openjfx/lib/javafx.media .jar /usr/share/openjfx/lib/javafx.swing.jar /usr/share/openjfx/lib/javafx.web.jar

通过包含 javafx 路径和模块来运行 jar 应用程序:
java --module-path $PATH_TO_OPENJFX-LIB --add-modules module_1,module_2,module_3,...,module_n -jar $PATH_TO_JAR_FILE

例子:

java --module-path /usr/share/openjfx/lib --add-modules=javafx.controls,javafx.fxml,javafx.base,javafx.media,javafx.web,javafx.swing -jar '/home/lotfi/Documents/MyAppfolder/my_application.jar'
  1. 如果某些时候 java 的版本与 jar 不同。罐子不起作用。

我想为java制作一个没有上述问题的安装程序。

即使用户没有在 Linux 操作系统上安装 java jdk,它也应该可以工作。

除此之外,我想在 linux os 的搜索选项中添加应用程序。

搜索栏

我怎样才能制作这样的应用程序?

帮助!!!

标签: javalinuxjavafx

解决方案


java有几个工具可以实现这一点。jlink允许您将 jre 与您的 java 文件打包在一起。有一个专门用于完成此任务的maven 插件。

如果您已经有一个有效的 maven/javafx 设置,那么您需要做的就是将插件添加到您的pom.xml

<plugin>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
       <version>0.0.4</version>
       <configuration>
       <mainClass>modulename/com.example.Main</mainClass>
       <launcher>nametolaunch</launcher>
    </configuration>
</plugin>

然后当你构建你的项目时。

mvn javafx:jlink

它将编译您的项目,并创建 jlink。在target文件夹中,您将有一个文件夹image,其中包含您需要的所有内容。您可以将其压缩并在任何兼容平台上使用。

运行应用程序image/bin/nametolaunch


推荐阅读