First Time Gradle


(diredko) #1

Hello) I’ve started to learn gradle two days ago. I’ve read some articles and several chapters in Gradle Guide. But when I tried to implement a tutorial project from http://spring.io/guides/gs/gradle/ I’ve got some complications. The problem is that I don’t know how to add dependencies so that they are available in runtime. At this point when I’am running the main class of that project I’am getting a NoClassDefFoundError indicating that the class I’ve used from that library is not available due to some reasons. Maybe the tutorial is only about to compile the project and it’s not supposed to be run. But I still need to figure it out. I’ve also created a Gradle project in NetBeans (it uses Spring Boot) it works within the IDE, but I still cant run it outside one.

So the main question is that: How can I compose build.gradle file so that the output classes and jars can access the external libraries (jars in particular)?

I’am getting:

Exception in thread "main" java.lang.NoClassDefFoundError: org/joda/time/LocalTime
        at hello.HelloWorld.main(HelloWorld.java:7)
Caused by: java.lang.ClassNotFoundException: org.joda.time.LocalTime
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        ... 1 more

Thank You)) P.S. Sorry for such a tedious explanations))


(René Groeschke) #2

Hello, It’s hard to say what went wrong without seeing the build.gradle file. Can you share your build script? Have you added this snippet

repositories { mavenCentral() }
dependencies {
  compile "joda-time:joda-time:2.2"
}

to your build script?

cheers, René


(diredko) #3

Yes I’ have added this part to my code. In fact I’ve made everything according to this http://spring.io/guides/gs/gradle/ tutorial without skipping any step (I’ve went through all steps up to one where I can build a project). apply plugin: ‘java’

repositories { mavenCentral() } dependencies {

compile “joda-time:joda-time:2.2” }


(René Groeschke) #4

How do you invoke the main class?


(diredko) #5

java hello.HelloWorld; Surely previously I made the current directory to be the one where the package is situated;


(René Groeschke) #6

you have to pass the classpath (including the jodatime jar). An easier way to run your program is to use the Run task of the gradle application plugin


(diredko) #7

Thank you) But another point is I don’t know where Gradle stores the dowmloaded dependencies; build/dependencie-cache folder is empty, so I can’t pass jodatime jar file within the classpath;


(Perryn Fowler) #8

Hi XanderMax,

The basic problem here is that the tutorial only goes as far as telling gradle how to build your project. It doesn’t tell gradle anything about how you want to run it. It looks like the writers of the tutorial expected you to run it in eclipse, but you want to run it from the command line, and gradle is giving you no help whatsoever with that.

So, the ‘RIGHT’ way to do this is to tell gradle that you want to run this from the command line using the application plugin:

apply plugin:'application'
mainClassName = "hello.HelloWorld"

Now you should be able to run

./gradlew installApp install

and gradle will install your application and all its dependencies to the ‘install’ directory, complete with scripts in ‘install/bin’ that allow you to run the app from the command line.

If don’t want to do it the “RIGHT” way you could do something hacky like this instead:

task showDependencies << {
  configurations.runtime.each { println it }
}
  task copyDependencies(type: Copy) {
  into 'dependencies'
  from configurations.runtime
}

the showDependencies task should show you where the runtime dependencies are cached and the copyDependencies will copy them into a ‘dependencies’ folder for you.


(diredko) #9

Thank you very much)))) while(true) System.out.println(“THANKS”);


(Peter Niederwieser) #10

It stores them somewhere under ~/.gradle, but you shouldn’t have to worry, and shouldn’t touch the cache.