应用程序在 Intellij 中运行良好,但在打包到 exe 文件中时不起作用

Application runs fine in Intellij but doesn't work when packaged into an exe file

我已经使用 picocli and I would like to package it into an executable file. I first packaged it into a jar file using Maven package, and then used Launch4J 构建了一个非常简单的命令行应用程序,正如几个用户在 SO 上推荐的那样,以获得可执行文件。但是,当我在命令提示符下 运行 Application.exe 时,它会抛出 NoClassDefFound 错误。

> Exception in thread "main" java.lang.NoClassDefFoundError:
> picocli/CommandLine                                                 
> at mycli.HelloCli.main(HelloCli.java:48)                              
> Caused by: java.lang.ClassNotFoundException: picocli.CommandLine      
> at java.net.URLClassLoader.findClass(Unknown Source)                  
> at java.lang.ClassLoader.loadClass(Unknown Source)                    
> at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)         
> at java.lang.ClassLoader.loadClass(Unknown Source)                    
> ... 1 more

该应用程序在 IntelliJ 中执行时运行良好。我尝试通过查看类似于 these 的答案来添加我创建的自定义 jar 文件,但它似乎没有帮助。我也试过更改执行文件夹,但这也没有解决错误。

从我在 SO 中搜索的几个答案中,我意识到 picocli jar 需要在 class 路径上。所以我添加了“Main class”作为包含我的 main 方法的那个,并指定了“类路径”到 jar 文件,该文件是通过从存在的所有依赖项中构建一个工件而创建的,在配置 Launch4J 时。

有关如何从项目的依赖项构建工件 jar 的详细信息,如果您使用 IntelliJ,请访问此answer if you use Eclipse, or this answer。)