adjhit
adjhit

Reputation: 45

Very simple Gradle Java Project, cannot find dependencies

I'm building a very basic java project to learn gradle and I'm having trouble adding dependencies

Here is my build.gradle:

apply plugin: 'java'
apply plugin: 'idea'


repositories {

  // JCenter (https://bintray.com/bintray/jcenter)
  jcenter()

  // Maven central repo
  mavenCentral()

}


dependencies {

  // Apache Commons Math 3.6.1
  compile 'org.apache.commons:commons-math3:3.6.1'

  // Google Guice 
  compile 'com.google.inject:guice:4.1.0'

}

And here is my file structure

project
├── build
├── build.gradle
└── src
    ├── main
    │   ├── java
    |   |   |__Main.java
    │   └── resources
    └── test
        ├── java
        └── resources

In Main.java I followed a simple commons math example:

import org.apache.commons.math3.stat;

public class Main {
    public static void main(String[] args) throws Exception {
        double[] values = new double[] {65, 51, 16, 11, 6519,
                                        191 , 0, 98, 19854, 1, 32};

        DescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();

        for (double v : values) {
            descriptiveStatistics.addValue(v);
        }
        double mean = descriptiveStatistics.getMean();
        double median = descriptiveStatistics.getPercentile(50);
        double standardDeviation = descriptiveStatistics.getStandardDeviation();

        System.out.println(mean + "\n" + median + "\n" + standardDeviation);
    }
}

However, when I try to build my project, I get the cannot find symbol error.

Main.java:1: error: cannot find symbol
import org.apache.commons.math3.stat;
                           ^
    symbol:   class stat

I'm not sure if it's a problem with Gradle or how I structured my project (when running 'gradle dependencies' everything seems fine).

How do I get this simple example to run?

Upvotes: 2

Views: 1692

Answers (1)

kevin628
kevin628

Reputation: 3526

The problem is this line at the top of your code file:

import org.apache.commons.math3.stat;

Since stat is a package and not a class, the compiler is unsure what that import is trying to do, so it errors out.

Instead, try pulling in the class you need. In this case, it looks like you want the DescriptiveStatistics class:

import org.apache.commons.math3.stat.descriptive.DescriptiveStatist‌​ics;

or import everything if most of the package is used

import org.apache.commons.math3.stat.descriptive.*;

Upvotes: 2

Related Questions