2

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 :

Error: Could not find or load main class net.nephty.connectwisemonitoring.app.Main
Caused by: java.lang.ClassNotFoundException: net.nephty.connectwisemonitoring.app.Main

The Main class contains the main method that runs the application. It works fine when being run, meaning there is no compile time error.

I am building this project using Gradle. Here is my build.gradle file :

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 = "18.0.2"
    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("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")

    implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.4.0'


}

test {
    useJUnitPlatform()
}

jlink {
    imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
    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'
        )
    }
}

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.

The manifest is being build properly, as all the specified attributes end up in the file.

I've looked up this, this, this and this post, as well as many many more but none seemed to solve my issue.

I've tried making a new project and re-writing my build.gradle file but to no avail.

When I use gradle jar, the code ends up in the jar, which is what I want. Unfortunately, gradle jar doesn't include dependencies AFAIK.

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 here.

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 :

    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }

where the runtimeClasspath would be wrong but I can't find anything about this.

Specs :

------------------------------------------------------------         
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

Thanks in advance for your time. Let me know if I missed some information I should share.

EDIT : I've tried changing the duplicate strategy but that didn't do anything.

Nephty
  • 140
  • 1
  • 7
  • You are only packaging the dependencies, but not the actual code/resources itself. Where did you get the code for the `uberJar` from? Why not use this plugin for creating a fat-jar https://github.com/johnrengelman/shadow ? – user16358266 Aug 23 '22 at 10:01
  • I don't quite remember, it's something I tried out but forgot to remove. What do you mean by using this plugin ? The build.gradle jar task or something more ? – Nephty Aug 23 '22 at 14:38
  • The shadow plugin deals with all the intricacies of creating a fat/uber jar, I'm suggesting to use it, instead of trying to create your own task for it. – user16358266 Aug 23 '22 at 14:42

0 Answers0