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 -

test
    ------lib
            ------junit-4.4.jar(with other jar files)
    ------code.jar

Here is my code -

apply plugin: 'java'
  repositories {
 flatDir{
    dirs 'D:/test/lib'
 }
}
  dependencies {
   compile group: 'junit', name: 'junit', version: '4.4'
   }
     task runTest(type:JavaExec){
   main = 'com.org.mainMethod'
 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 java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class:com.org.mainMethod
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')