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

357 Views Asked by At

I have built a very simple command line application using 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 as recommended by several users on SO to obtain an executable. However, it throws the NoClassDefFound error, when I run Application.exe in the command prompt.

> 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

The application works perfectly well when executed in IntelliJ. I tried adding the custom jar file I created by looking at answers similar to these but it doesn't seem to help. I also tried changing the folder of execution but that doesn't solve the error either.

1

There are 1 best solutions below

0
On BEST ANSWER

From several answers I searched across SO, I realized that the picocli jar needed to be on the class path. So I added the "Main class" as the one containing my main method, and specified the "Classpath" to the jar file created by building an artifact out of all the dependencies present, while configuring Launch4J.

For details on how to build an artifact jar out of a project's dependencies, visit this answer if you use Eclipse, or this answer if you use IntelliJ.)

Launch4J configuration window