启动用jpackage创建的已安装可执行文件时出现ClassNotFoundException



我生成MSI安装程序的过程如下。

  1. 运行mvn install以生成Example-1.0-SNAPSHOT-jar-with-dependencies.jar
  2. 运行windows-executable.bat生成Example-1.0.0.msi
  3. 安装Example-1.0.0.msi
  4. 转到安装目录,打开一个终端,然后运行Example-1.0.0.exe,查看阻止应用程序启动的任何错误

此POM文件(删除了不相关的部分(在mvn install期间用于生成Example-1.0-SNAPSHOT-jar-with-dependencies.jar

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.Valkryst</groupId>
<artifactId>Example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<repositories>
<!-- REMOVED -->
</repositories>
<dependencies>
<!-- REMOVED -->
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>res</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--add-exports=java.desktop/sun.java2d.d3d=ALL-UNNAMED</arg>
<arg>--add-exports=java.desktop/sun.java2d.pipe.hw=ALL-UNNAMED</arg>
<arg>--add-exports=java.desktop/java.awt.peer=ALL-UNNAMED</arg>
<arg>--add-exports=java.desktop/sun.java2d=ALL-UNNAMED</arg>
</compilerArgs>
<fork>true</fork>
</configuration>
</plugin>
<!-- Used, during the test phase of the build, to run the unit tests -->
<plugin>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire</artifactId>
<version>2.21.0</version>
</plugin>
<!-- Used to include all of the dependencies in the packaged Jar file -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.valkryst.Example.Driver</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</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>
</project>

此脚本使用jpackage生成MSI安装程序。

mkdir temp
mklink "%~dp0tempExample-1.0-SNAPSHOT-jar-with-dependencies.jar" "%~dp0....targetExample-1.0-SNAPSHOT-jar-with-dependencies.jar"
"C:Program FilesJavajdk-14binjpackage.exe" ^
--type msi ^
--app-version "1.0.0" ^
--copyright "Valkryst, 2020" ^
--name "Example" ^
--vendor "Valkryst" ^
--verbose ^
--icon "%~dp0....resiconicon.ico" ^
--input "%~dp0temp" ^
--java-options "-Djdk.module.illegalAccess=permit" ^
--main-class "com.valkryst.Example.Driver" ^
--main-jar "Example-1.0-SNAPSHOT-jar-with-dependencies.jar" ^
--win-dir-chooser ^
--win-shortcut ^
--win-console
del "%~dp0tempExample-1.0-SNAPSHOT-jar-with-dependencies.jar"
rmdir "%~dp0temp
pause

这是步骤#4的输出。

PS C:Program FilesExample> .Example.exe
Error: Could not find or load main class com.valkryst.Example.Driver
Caused by: java.lang.ClassNotFoundException: com.valkryst.Example.Driver

经过进一步调查,jpackage实用程序似乎不支持符号链接。它没有将Example-1.0-SNAPSHOT-jar-with-dependencies.jar打包到可执行文件中,因此安装的Example-1.0.0.exe无法加载主Driver类,因为它不在安装目录中。

mkdir temp
copy "%~dp0....targetExample-1.0-SNAPSHOT-jar-with-dependencies.jar" "%~dp0tempExample-1.0-SNAPSHOT-jar-with-dependencies.jar"
"C:Program FilesJavajdk-14binjpackage.exe" ^
--type msi ^
--app-version "1.0.0" ^
--copyright "Valkryst, 2020" ^
--name "Example" ^
--vendor "Valkryst" ^
--verbose ^
--icon "%~dp0....resiconicon.ico" ^
--input "%~dp0temp" ^
--java-options "-Djdk.module.illegalAccess=permit" ^
--main-class "com.valkryst.Example.Driver" ^
--main-jar "Example-1.0-SNAPSHOT-jar-with-dependencies.jar" ^
--win-dir-chooser ^
--win-shortcut ^
--win-console
rmdir /S /Q "%~dp0temp
pause

相关内容

  • 没有找到相关文章

最新更新