0

I hope title itself is descriptive but to be clear I am trying to include one of the jar(ie, error-handling-service.jar) in greeting-service.jar. After build I included greeting-service.jar in new project(ie,TestApplication) but on executing the TestApplication I am getting(BTW, TestApplication is not a gradle project)

Exception in thread "main" java.lang.NoClassDefFoundError: co/common/exception/BaseException
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763)

co.common.exception.BaseException is a class in error-handling-service module

As per question here. I included

manifest {
            attributes(
                    "Manifest-Version": "1.0",
                    "Class-Path": configurations.compile.collect { it.getName() }.join(' ')
            )
        }

Here is build.gradle of greeting-service which is dependent on error-handling service

buildscript {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile('org.apache.commons:commons-lang3:3.0')
    compile('org.apache.commons:commons-collections4:4.0')
    compile('org.slf4j:slf4j-api:1.7.25')
    compile('co.common:error-handling-service:1.0.0-SNAPSHOT')
    testCompile("org.mockito:mockito-all:1.9.5")
    testCompile('junit:junit:4.12')
}

jar {
    baseName = 'greeting-service'
    version = '1.0.0-SNAPSHOT'
    manifest {
        attributes(
                "Manifest-Version": "1.0",
                "Class-Path": configurations.compile.collect { it.getName() }.join(' ')
        )
    }
}

group = 'co.common'
version = '1.0.0-SNAPSHOT'

sourceCompatibility = 1.8
targetCompatibility = 1.8


task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

After successful build of greeting-service, I included greeting-service.jar in TestApplication and still I received the same exception mentioned above.

Manifest-Version: 1.0
Class-Path: commons-lang3-3.0.jar commons-io-2.4.jar commons-collections
 4-4.0.jar slf4j-api-1.7.25.jar error-handling-service-1.0.0-SNAPSHOT.
 jar commons-logging-1.1.3.jar

Why is this happening and what should be done ?

balboa_21
  • 375
  • 1
  • 7
  • 21

2 Answers2

0

NoClassDefFoundError in Java comes when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. For example, if we have a method call from a class or accessing any static member of a Class and that class is not available during run-time then JVM will throw NoClassDefFoundError.

So, assuming your gradle build passes successfull - I may conclude your are providing wrong error-handling-service-1.0.0-SNAPSHOT.jar, which doesn't contain co/common/exception/BaseException class.

You should carefully check actual contents of this jar.

Ivan Pronin
  • 1,768
  • 16
  • 14
  • contents of jar does not contain the classes of error-handling-service-1.0.0-SNAPSHOT.jar. But that is what I want to know does my config look good or I am missing something ? – balboa_21 Aug 11 '17 at 06:07
  • Your build file looks ok. But the error is caused by missing class `co/common/exception/BaseException`. So, you should include .jar file with this class into build path. Or you can remove usage of this class from your code. You can check actual dependencies of your gradle project using `gradle dependencies` command – Ivan Pronin Aug 11 '17 at 20:04
0

Not sure if this will help but, if your error-handling-service.jar is already packed and ready to include as a dependency into your greeting-service.jar, then you should just make it a dependency.

For example:

  1. In your project, create a folder called "lib"
  2. Add your error-handling-service.jar in the "lib" folder
  3. Add the following to your dependencies block:

    dependencies { compile fileTree(dir: 'lib', include: ['*.jar']) ... }

Basically, every *.jar that you put into the "lib" folder will be added to your application jar when you run the build.

Hermann Steidel
  • 1,000
  • 10
  • 18
  • I wanted to avoid users of greeting-service.jar to include dependencies of greeting-service again. My jar would be plug and play. – balboa_21 Aug 11 '17 at 06:00