Problems building java project with dependencies

(Giovanni Bacci) #1

Hi everyone,
I’m quite new to Gradle so I hope you will forgive me if I say something obvious. I’m building a java project just to get used to the Gradle dependency system but I have encountered a problem whit dependencies.

Here is the content of the file generated by Eclipse and modified by me:


  • This build file was auto generated by running the Gradle ‘init’ task
  • by ‘giovanni’ at ‘11/9/16 3:16 PM’ with Gradle 3.0
  • This generated file contains a sample Java project to get you started.
  • For more details take a look at the Java Quickstart chapter in the Gradle
  • user guide available at

// Apply the java plugin to add support for Java
apply plugin: ‘java’

description = "Testing dependencies"
version = 0.1

sourceCompatibility = 1.8
targetCompatibility = 1.8

// In this section you declare where to find the dependencies of your project
repositories {
// Use ‘jcenter’ for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.

// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile ‘org.slf4j:slf4j-api:1.7.21’

// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
runtime 'org.apache.commons:commons-math3:3.6.1'


jar {
manifest {
‘Main-Class’: ‘testing.TestingDep’

And this is the ocntent of my main class

package testing;

import org.apache.commons.math3.stat.inference.ChiSquareTest;

public class TestingDep {

public static void main(String[] args) {
System.out.println(“Hello World”);

  ChiSquareTest test = new ChiSquareTest();
  long[][] dist = {{1,2,3,4},{5,6,7,8}};



And this is the message that I get when I try to build it:

…/test-dependencies/src/main/java/testing/ error: package org.apache.commons.math3.stat.inference does not exist
import org.apache.commons.math3.stat.inference.ChiSquareTest;
…/test-dependencies/src/main/java/testing/ error: cannot find symbol
ChiSquareTest test = new ChiSquareTest();
symbol: class ChiSquareTest
location: class TestingDep
…/test-dependencies/src/main/java/testing/ error: cannot find symbol
ChiSquareTest test = new ChiSquareTest();
symbol: class ChiSquareTest
location: class TestingDep
3 errors
:compileJava FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:compileJava’.

Compilation failed; see the compiler error output for details.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.


Total time: 0.755 secs

I tried to build the jar and run it, but nothing happened, if I set the dependency level to “compile” the project is built but when I launch it I get the java.lang.NoClassDefFoundError.

Any help will be appreciated,

thanks in advance,


(James Justinic) #2

You should be using compile instead of runtime because you do have a compilation dependency on the commons math library. The NoClassDefFoundError is the one you need to focus on. The built jar is correct, just not adequate for what you likely want to do with it.

If you’re just running the built jar, you’re still missing your dependencies on the runtime classpath (Gradle isn’t involved at that point). You can use the application plugin to generate a bundle with start scripts that will appropriate include everything that you need. If you’re expecting to just run the single built jar, you’ll need to build a fat jar instead. This isn’t default behavior so you would want to add a plugin for this or do some additional manual configuration.