0

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?

Gatonito
  • 1,662
  • 5
  • 26
  • 55
  • You need jsoup on the classpath, and your `-cp` doesn't have that. See http://stackoverflow.com/q/21358466/5221149 for how to run your program using Gradle, so classpath is handled for you. – Andreas Jul 29 '16 at 02:53
  • @Andreas I didn't understand. Why can't I simply run it? Why gradle won't compile org.json in the right folders? I tought gradle was meant to automatically do all these things. :(. What are those execute tasks? I'm lost – Gatonito Jul 29 '16 at 04:09
  • Jsoup is an external library *used* by your code. Gradle is not supposed to compile that. It's supposed to compile *your* code, and it did. To *run* your code, you need your code (packages in `workspace.jar` by Gradle for you) and any external libraries needed, e.g. Jsoup's Jar file. It is only Gradles job to run the code if you tell it to, like explained in the link I gave. – Andreas Jul 29 '16 at 04:27
  • @Andreas Why workspace.jar? I've run the .classes file and it worked, but it stopped at the part of the jsoup. I tought it'd compile with jsoup because in Android Studio I don't need to download the .jar and include them, I just say to gradle that it's a dependency. Why is the dependencies { compile 'org.jsoup:jsoup:1.8.3' } for, then? – Gatonito Jul 29 '16 at 05:03
  • The Gradle script downloaded Jsoup for you and added it to the classpath when compiling your code. That's what `compile 'org.jsoup:jsoup:1.8.3'` means: It is required (a dependency) for compilation. --- You can run with either `classes` or `workspace.jar`, assuming your jar only contains .class files. If you have more than .class files, then `classes` won't work, because *it* only has the .class files, and not the other files needed. – Andreas Jul 29 '16 at 05:08
  • @Andreas thanks, I understood. Isn't there a way to run with gradle automatically including the jsoup in the claspath? Or a way to compile and include jsoup.jar in the build directory? – Gatonito Jul 29 '16 at 05:09
  • Yes, there is a way to run with gradle. See [previous link](http://stackoverflow.com/questions/38649551?noredirect=1#comment64681906_38649551). You can get dependency jar files added to the build folder if you add a copy task to the build script. See http://stackoverflow.com/q/14669809/5221149. – Andreas Jul 29 '16 at 05:11
  • @Andreas thanks, I understood! But why Android Studio didn't need all of this? I'm asking because this is a library that I'll use in ANdroid Studio but I'd like to compile it alone first to do some tests, I'm afraid this new gradle.build file will break the building in Android Studio after. What are the precautions I must take? – Gatonito Jul 29 '16 at 05:27
  • Because Android Studio understands the Gradle script, and sets up the classpath accordingly. – Andreas Jul 29 '16 at 05:28

0 Answers0