1

I have an average-sized project and I plan to create an uber-jar. I compile it with Intellij and built an artefact with "JAR files from libraries" set to "extract to the target JAR".

Here is the outcome:

java -cp MyJar.jar com.package.MainClass
Error: Could not find or load main class com.package.MainClass

java -Xdiag -cp MyJar.jar com.package.MainClass
Error: Could not find or load main class com.package.MainClass
Caused by: java.lang.ClassNotFoundException: com.package.MainClass
java.lang.ClassNotFoundException: com.package.MainClass
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:415)
    at java.base/sun.launcher.LauncherHelper.loadMainClass(LauncherHelper.java:770)
    at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:655)

If I list jar's content, the class is there. If I extract the jar and run the class from the filesystem, it works:

jar xf MyJar.jar
java com.package.MainClass <-- it works

If, in IntelliJ, I set "JAR files from libraries" to "Copy the output and link via manifest" the jar does work, but I get a bunch of smaller jar files in target directory.

Does anyone have any idea what could go wrong and how should I debug it?

Naman
  • 27,789
  • 26
  • 218
  • 353
user3429660
  • 2,420
  • 4
  • 25
  • 41

1 Answers1

0

After many trials, it seems that including some JARs cause the problem: power-mock, objenesis and mockito.

user3429660
  • 2,420
  • 4
  • 25
  • 41