Cannot find junit and main method

(Priyanka Gopinath) #1

Hi, I am new to gradle projects. My task when run, can’t find the junit related classes nor the main method. I want to use the junit that is bundled as a jar in the lib folder. I have a project structure like this -

            ------junit-4.4.jar(with other jar files)

Here is my code -

apply plugin: 'java'
  repositories {
    dirs 'D:/test/lib'
  dependencies {
   compile group: 'junit', name: 'junit', version: '4.4'
     task runTest(type:JavaExec){
   main = ''
 classpath = files([repositories.flatDir,'D:/test/code.jar','D:/test/lib'])

The task fails because it cannot find the junit related files. I am completely lost now -

java.lang.NoClassDefFoundError: org/junit/runner/notification/RunListener
Caused by: java.lang.ClassNotFoundException: org.junit.runner.notification.RunListener
        at Method)
        at java.lang.ClassLoader.loadClass(
        at sun.misc.Launcher$AppClassLoader.loadClass(
        at java.lang.ClassLoader.loadClass(
Could not find the main
Program will exit.

Any help is appreciated.

(Peter Niederwieser) #2

‘flatDir’ dependencies don’t need a ‘group’, although it may not be a problem to declare one. Try to omit the group or use the shorthand notation (‘compile “:junit:4.4”’). The ‘classpath’ configuration for the ‘runTest’ task is incorrect (you can’t put a repository or a directory on a Java class path). If these are JUnit tests, you should probably use the ‘test’ task provided by the ‘java’ plugin instead.

(Priyanka Gopinath) #3

Thanks for the reply Peter. The biggest issue was that the jar files under the d:/test/lib were not getting included. After adding a dependency to include all jar files, the problem got resolved.

Also fixed the classpath as suggested :slight_smile:

javaRun fileTree(dir: 'D:/test/lib', include: '*.jar')