英文:
ClassNotFoundException when launching an installed executable created with jpackage
问题
以下是翻译好的内容:
生成 MSI 安装程序的过程如下:
- 运行 
mvn install以生成Example-1.0-SNAPSHOT-jar-with-dependencies.jar。 - 运行 
windows-executable.bat以生成Example-1.0.0.msi。 - 安装 
Example-1.0.0.msi。 - 前往安装目录,打开终端,运行 
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>
    <!-- 其他属性,依赖项和构建配置已省略 -->
</project>
此脚本使用 jpackage 生成 MSI 安装程序。
mkdir temp
mklink "%~dp0temp\Example-1.0-SNAPSHOT-jar-with-dependencies.jar" "%~dp0..\..\target\Example-1.0-SNAPSHOT-jar-with-dependencies.jar"
"C:\Program Files\Java\jdk-14\bin\jpackage.exe" ^
--type msi ^
--app-version "1.0.0" ^
--copyright "Valkryst, 2020" ^
--name "Example" ^
--vendor "Valkryst" ^
--verbose ^
--icon "%~dp0..\..\res\icon\icon.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 "%~dp0temp\Example-1.0-SNAPSHOT-jar-with-dependencies.jar"
rmdir "%~dp0temp"
pause
这是步骤#4的输出。
PS C:\Program Files\Example> .\Example.exe
Error: Could not find or load main class com.valkryst.Example.Driver
Caused by: java.lang.ClassNotFoundException: com.valkryst.Example.Driver
英文:
My process for generating the MSI installer is as follows.
- Run 
mvn installto generateExample-1.0-SNAPSHOT-jar-with-dependencies.jar. - Run 
windows-executable.batto generateExample-1.0.0.msi. - Install 
Example-1.0.0.msi. - Go to the install directory, open a terminal, and run 
Example-1.0.0.exeto see any errors preventing the application from launching. 
This POM file (irrelevant sections removed) is used during mvn install to generate 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>
This script uses jpackage to generate the MSI installer.
mkdir temp
mklink "%~dp0temp\Example-1.0-SNAPSHOT-jar-with-dependencies.jar" "%~dp0..\..\target\Example-1.0-SNAPSHOT-jar-with-dependencies.jar"
"C:\Program Files\Java\jdk-14\bin\jpackage.exe" ^
--type msi ^
--app-version "1.0.0" ^
--copyright "Valkryst, 2020" ^
--name "Example" ^
--vendor "Valkryst" ^
--verbose ^
--icon "%~dp0..\..\res\icon\icon.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 "%~dp0temp\Example-1.0-SNAPSHOT-jar-with-dependencies.jar"
rmdir "%~dp0temp
pause
This is the output from step #4.
PS C:\Program Files\Example> .\Example.exe
Error: Could not find or load main class com.valkryst.Example.Driver
Caused by: java.lang.ClassNotFoundException: com.valkryst.Example.Driver
答案1
得分: 0
经进一步调查,看起来 jpackage 实用程序不支持符号链接。它未将 `Example-1.0-SNAPSHOT-jar-with-dependencies.jar` 打包到可执行文件中,因此安装的 `Example-1.0.0.exe` 无法加载主 `Driver` 类,因为它在安装目录中不存在。
mkdir temp
copy "%~dp0..\..\target\Example-1.0-SNAPSHOT-jar-with-dependencies.jar" "%~dp0temp\Example-1.0-SNAPSHOT-jar-with-dependencies.jar"
"C:\Program Files\Java\jdk-14\bin\jpackage.exe" ^
--type msi ^
--app-version "1.0.0" ^
--copyright "Valkryst, 2020" ^
--name "Example" ^
--vendor "Valkryst" ^
--verbose ^
--icon "%~dp0..\..\res\icon\icon.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
英文:
After further investigation, it appears as though the jpackage utility doesn't support symbolic links. It wasn't packaging the Example-1.0-SNAPSHOT-jar-with-dependencies.jar into the executable, so the installed Example-1.0.0.exe was unable to load the main Driver class because it didn't exist in the installation directory.
mkdir temp
copy "%~dp0..\..\target\Example-1.0-SNAPSHOT-jar-with-dependencies.jar" "%~dp0temp\Example-1.0-SNAPSHOT-jar-with-dependencies.jar"
"C:\Program Files\Java\jdk-14\bin\jpackage.exe" ^
--type msi ^
--app-version "1.0.0" ^
--copyright "Valkryst, 2020" ^
--name "Example" ^
--vendor "Valkryst" ^
--verbose ^
--icon "%~dp0..\..\res\icon\icon.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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论