NoClassDefFoundError -- missing library


(Thufir) #1

The JAR isn’t found:

thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ gradle clean build;java -jar build/libs/hello_client.jar
Changed strategy of configuration ':compile' after it has been resolved. This behaviour has been deprecated and is scheduled to be removed in Gradle 3.0
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build

BUILD SUCCESSFUL

Total time: 1.078 secs
Exception in thread "main" java.lang.NoClassDefFoundError: net/bounceme/mordor/hello/HelloLibrary
    at net.bounceme.mordor.hello.Client.foo(Client.java:13)
    at net.bounceme.mordor.hello.Client.main(Client.java:9)
Caused by: java.lang.ClassNotFoundException: net.bounceme.mordor.hello.HelloLibrary
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more
thufir@mordor:~/NetBeansProjects/hello_client$ 

it’s not in build:

thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ tree build
build
├── classes
│   └── main
│       └── net
│           └── bounceme
│               └── mordor
│                   └── hello
│                       └── Client.class
├── dependency-cache
├── distributions
│   ├── hello_client.tar
│   └── hello_client.zip
├── libs
│   └── hello_client.jar
├── scripts
│   ├── hello_client
│   └── hello_client.bat
└── tmp
    ├── compileJava
    │   └── emptySourcePathRef
    └── jar
        └── MANIFEST.MF

14 directories, 7 files
thufir@mordor:~/NetBeansProjects/hello_client$ 

but it is in ~/.gradle:

thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ ll /home/thufir/.gradle/caches/modules-2/files-2.1/com.github.THUFIR/hello_api/latest/f78962c5e6db6f58bf4c1dfb088e29f0abaf3e0f/hello_api-latest.jar 
-rw-rw-r-- 1 thufir thufir 1227 Mar 10 01:47 /home/thufir/.gradle/caches/modules-2/files-2.1/com.github.THUFIR/hello_api/latest/f78962c5e6db6f58bf4c1dfb088e29f0abaf3e0f/hello_api-latest.jar
thufir@mordor:~/NetBeansProjects/hello_client$ 

Now, how do I actually use it, because the compile fails.

build file:

apply plugin: 'application'
apply plugin: 'maven'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
if (!hasProperty('mainClass')) {
    ext.mainClass = 'Client'
}

mainClassName = 'net.bounceme.mordor.hello.Client'

repositories {
    mavenCentral()
    maven { url "https://jitpack.io" }

}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.10'
    compile 'com.github.THUFIR:hello_api:latest'

}



jar {
    manifest {
        attributes ('Main-Class': 'net.bounceme.mordor.hello.Client',
            "Class-Path": configurations.compile.collect { it.getName() }.join(' '))
    }
}

assemble.dependsOn (jar)

configurations.all {
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}


(Benjamin Muschko) #2

Where is the missing class net.bounceme.mordor.hello.library.HelloLibrary coming from? Is it in the artifact of the declared dependency com.github.THUFIR:hello_api:latest? If that’s the case the command java -jar build/libs/hello_client.jar doesn’t know anything about the external library even though you might have it declared in the build. You will need to provide it as another JAR for the -jar argument. If you want some automatism there that also adds external libraries to the runtime classpath then have a look at the Application plugin.


(Thufir) #3

Quick reply, I haven’t googled or read much yet.

I did look at

https://docs.gradle.org/current/dsl/org.gradle.language.assembler.tasks.Assemble.html

but nothing jumped out at me. Yes, your understanding is exactly correct. The artifact of the declared dependency com.github.THUFIR:hello_api:latest and it’s absolutely an external library.

How do I provide it as another JAR? Specifically, where is it? I know that it’s in ~/.gradle, but that seems awkward.

I will have to look closer at the Application plugin, probably this functionality is there, I just need to find it. The installDist looks intriguing.