Why can't I simply build this gradle with jsoup dependency and run it?


(Guerlando OC) #1

I just built a simple java project with this gradle file:

apply plugin: 'java'

allprojects {
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.jsoup:jsoup:1.8.3'
}

then, it generates:

build/java/classes/main
build/tmp
build/libs
build/dependency-cache

where build is inside workspace

I then run java -cp build/classes/main MainSisgrad and it works, but when it come to the part of the jsoup interpretation, I get:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup   

Note that the folder build/libs has a file names workspace.jar, whats is it? Is it a .jar with all the libs compiled? Including jsoup? Because I tried to include it in the classpath like this:

java -cp build/libs/workspace.jar -cp build/classes/main MainSisgrad 

or

java -cp build/libs/ -cp build/classes/main MainSisgrad 

but it didn’t work. What’s missing?


(Dimitar Dimitrov) #2

What’s missing?

Gradle’s java plugin just creates a jar. In particular, it does not automatically bundle the dependencies for you.

Have a look at the application plugin https://docs.gradle.org/current/userguide/application_plugin.html which is probably what you are looking for.

If you are looking to create a single jar, have a look at the shadow plugin.


(Guerlando OC) #3

but where I’d put that it’s a java program? Or I put this below “apply plugin: java”?


(Dimitar Dimitrov) #4

Copy this: https://github.com/gradle/gradle/blob/master/subprojects/docs/src/samples/application/build.gradle