title: NoClassDefFoundError : cannot find main class in jar file
body: <p>After countless days of trying to find an answer to my question, I've decided to post it here. I've been developping a monitoring application for my company, and as I'm about to distribute it, the jar file seems not runnable. The exact error is :</p>
  <pre><code>Error: Could not find or load main class net.nephty.connectwisemonitoring.app.Main
  Caused by: java.lang.ClassNotFoundException: net.nephty.connectwisemonitoring.app.Main
  </code></pre>
  <p>The Main class contains the main method that runs the application. It works fine when being run, meaning there is no compile time error.</p>
  <p>I am building this project using Gradle. Here is my build.gradle file :</p>
  <pre><code>plugins {
      id 'java'
      id 'application'
      id 'org.openjfx.javafxplugin' version '0.0.10'
      id 'org.beryx.jlink' version '2.24.1'
  }
  
  group 'net.nephty'
  version '1.0-SNAPSHOT'
  
  repositories {
      mavenCentral()
  }
  
  ext {
      junitVersion = '5.9.0'
  }
  
  sourceCompatibility = '17'
  targetCompatibility = '17'
  
  tasks.withType(JavaCompile) {
      options.encoding = 'UTF-8'
  }
  
  application {
      mainModule = 'net.nephty.connectwisemonitoring'
      mainClass = 'net.nephty.connectwisemonitoring.app.Main'
  }
  
  javafx {
      version = &quot;18.0.2&quot;
      modules = ['javafx.base', 'javafx.controls', 'javafx.fxml', 'javafx.graphics', 'javafx.media', 'javafx.swing', 'javafx.web']
  }
  
  dependencies {
      implementation('org.controlsfx:controlsfx:11.1.1')
      implementation('com.dlsc.formsfx:formsfx-core:11.5.0') {
          exclude(group: 'org.openjfx')
      }
  
      implementation('org.kordamp.ikonli:ikonli-javafx:12.3.1')
  
      testImplementation(&quot;org.junit.jupiter:junit-jupiter-api:${junitVersion}&quot;)
      testRuntimeOnly(&quot;org.junit.jupiter:junit-jupiter-engine:${junitVersion}&quot;)
  
      implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.4.0'
  
  
  }
  
  test {
      useJUnitPlatform()
  }
  
  jlink {
      imageZip = project.file(&quot;${buildDir}/distributions/app-${javafx.platform.classifier}.zip&quot;)
      options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
      launcher {
          name = 'app'
      }
  }
  
  jlinkZip {
      group = 'distribution'
  }
  
  task fatJar(type: Jar) {
      duplicatesStrategy = DuplicatesStrategy.EXCLUDE
      manifest {
          attributes 'Start-Class': 'net.nephty.connectwisemonitoring.app.Main'
          attributes 'Main-Class': 'net.nephty.connectwisemonitoring.app.Main'
      }
      from {
          configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
      }
      exclude 'META-INF/*.RSA'
      exclude 'META-INF/*.SF'
      exclude 'META-INF/*.DSA'
  }
  
  jar {
      manifest {
          attributes(
                  'Main-Class': 'net.nephty.connectwisemonitoring.app.Main'
          )
      }
  }
  </code></pre>
  <p>Because of the error I've had, I've tried unpacking the jar to see if my code was in the right location. When unpacking the jar, I see that there is no net/nephty/... directory and cannot find my code anywhere.</p>
  <p>The manifest is being build properly, as all the specified attributes end up in the file.</p>
  <p>I've looked up <a href="https://stackoverflow.com/questions/16868446/javafx-exception-in-thread-main-java-lang-noclassdeffounderror-javafx-applica">this</a>, <a href="https://stackoverflow.com/questions/70489776/im-encounter-a-java-lang-noclassdeffounderror-and-i-dont-know-why-its-occurin">this</a>, <a href="https://stackoverflow.com/questions/49278063/how-do-i-create-an-executable-fat-jar-with-gradle-with-implementation-dependenci">this</a> and <a href="https://stackoverflow.com/questions/51455197/gradle-fatjar-could-not-find-or-load-main-class">this</a> post, as well as many many more but none seemed to solve my issue.</p>
  <p>I've tried making a new project and re-writing my build.gradle file but to no avail.</p>
  <p>When I use <code>gradle jar</code>, the code ends up in the jar, which is what I want. Unfortunately, <code>gradle jar</code> doesn't include dependencies AFAIK.</p>
  <p>If you'd like a minimal reproducible example, you can try creating a new project using gradle and use my build.gradle file as well as a basic main class located in the correct path. If you'd like to have the code sample, I've uploaded it <a href="https://transfer.sh/O18qVk/ConnectwiseMonitoring.zip" rel="nofollow noreferrer">here</a>.</p>
  <p>I feel like I'm really really close to solving this, but I can't find the source of the issue. My best guess would be that it stems from this part of the build.gradle file :</p>
  <pre><code>    from {
          configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
      }
  </code></pre>
  <p>where the runtimeClasspath would be wrong but I can't find anything about this.</p>
  <p>Specs :</p>
  <pre><code>------------------------------------------------------------         
  Gradle 7.5                                                           
  ------------------------------------------------------------         
                                                                       
  Build time:   2022-07-14 12:48:15 UTC                                
  Revision:     c7db7b958189ad2b0c1472b6fe663e6d654a5103               
                                                                       
  Kotlin:       1.6.21                                                 
  Groovy:       3.0.10                                                 
  Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
  JVM:          17.0.4 (Oracle Corporation 17.0.4+11-LTS-179)
  OS:           Windows 10 10.0 amd64
  </code></pre>
  <p>Thanks in advance for your time. Let me know if I missed some information I should share.</p>
  <p>EDIT : I've tried changing the duplicate strategy but that didn't do anything.</p>