0

Edit: This question is a duplicate to this question, which got incorrectly marked as duplicate, forcing me to restate my question.

To preface this, yes, I have seen this thread, but it did not help me. Specificly, while multiple people mentioned that different class structures between the building branch and the environment branch can exists, noone said anything about how to test for that, let alone fix it. And even then, I am not certain that this is really root cause.

I am working on a gradle project and I wanted to compile the project I had, to test it outside of my IDE, Netbeans 8.2, in which it works fine.

When I try to run the program with the usual java -jar command, it initally works fine, until the programs comes across a class that can throw a JDOMException. Then I get the error described in the title, followed by a NoClassDefFoundError.

In Netbeans, the JDOMException.class is included under Dependencies>Compile for main>jdom2-2.0.6.jar>org.jdom2

And the build.gradle script reads:

    apply plugin: 'java'

sourceCompatibility = 1.8

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'


if (!hasProperty('mainClass')) {
    ext.mainClass = 'bikesys.hda.BikeSys'
}

repositories {
    mavenCentral()
    jcenter()
}

dependencies {

testCompile group: 'junit', name: 'junit', version: '4.10'
compile files('libs/jcalendar-1.4.jar')
compile group: 'org.jdom', name: 'jdom2', version: '2.0.6'


compile ("com.byteowls:jopencage:1.2.0")
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
compile group: 'org.slf4j', name:'slf4j-api', version: '1.7.2'
compile group: 'ch.qos.logback', name:'logback-classic', version: '1.0.9'
compile group: 'ch.qos.logback', name:'logback-core', version: '1.0.9'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.6'

compile group: 'org.json', name: 'json', version: '20180813'

compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
compile group: 'log4j', name: 'log4j', version: '1.2.17'
compile group: 'me.atlis', name: 'atlis-location-base', version: '1.0.0'    
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.8.1'
compile group: 'me.atlis', name: 'nominatim-api', version: '1.0.0'
}

jar {
    manifest {
        attributes(
            'Main-Class': 'bikesys.hda.BikeSys'
         )
    }
}

I am using java jdk 1.8.0_202 in Netbeans and jre 1.8.0_211-b12 as runtime environment. I have found answers along the lines of "You have to add jdom2 to your runtime environment", however I neither know how to do that, nor if that is a general solution to the problem.

I'd appreciate the help!

1 Answers1

0

The problem was that, by default, gradle does not include external dependencies when building a normal .jar file. I had to instruct gradle to build a "fat jar" file instead.

I did this by altering the

jar {
    ...
}

part of my gradle script into the following instruction:

jar {
    manifest {
        attributes(
                'Main-Class': 'bikesys.hda.BikeSys'
        )
    }
    from {
        configurations.compileClasspath.filter{ it.exists() }.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

Be sure to edit the class path, which in my case was bikesys.hda.BikeSys, to your main class.