Search code examples
grailsgroovyimportjfreechart

Grails JFreeChart unable to resolve class: org.jfree.chart.ChartFactory


I am running a program to draw a line chart using JFreeChart library, and Groovy/Grails back end. Here is my code:

    package com.xyz.jfreechartdemo

    import org.jfree.chart.ChartFactory
    import org.jfree.chart.JFreeChart
    import org.jfree.chart.plot.PlotOrientation
    import org.jfree.data.category.DefaultCategoryDataset

    class JFreeChartSample {
      def getChart(){
      def dataset = createDataset()
      def chart = createChart(dataset)
      return chart
    }

    def createDataset() {
       DefaultCategoryDataset dataset = new DefaultCategoryDataset()
      dataset.addValue(2000,"X",1)
      dataset.addValue(3300,"X",2)
      dataset.addValue(2000,"X",3)
      dataset.addValue(3300,"X",4)
      return dataset
    }

   def createChart(dataset){
    JFreeChart chart = ChartFactory.createLineChart("Widget not rendered","X","Count",dataset,PlotOrientation.VERTICAL,false,true,false)
    return chart
  }
}

I am working on IntelliJ Idea 13.1.3. Compilation was successful. But here's what I see when I run the program:

Compilation error: startup failed:
C:\Users\User\IdeaProjects\JFreeChartDemo\grails app\domain\com\xyz\jfreechartdemo\JFreeChartSample.groovy: 3: unable to resolve class org.jfree.chart.ChartFactory
 @ line 3, column 1.
   import org.jfree.chart.ChartFactory
   ^

C:\Users\User\IdeaProjects\JFreeChartDemo\grails-app\domain\com\xyz\jfreechartdemo\JFreeChartSample.groovy: 6: unable to resolve class org.jfree.data.category.DefaultCategoryDataset
 @ line 6, column 1.
   import org.jfree.data.category.DefaultCategoryDataset
    ^

C:\Users\User\IdeaProjects\JFreeChartDemo\grails-app\domain\com\xyz\jfreechartdemo\JFreeChartSample.groovy: 5: unable to resolve class org.jfree.chart.plot.PlotOrientation
 @ line 5, column 1.
   import org.jfree.chart.plot.PlotOrientation
  ^

C:\Users\User\IdeaProjects\JFreeChartDemo\grails-app\domain\com\xyz\jfreechartdemo\JFreeChartSample.groovy: 4: unable to resolve class org.jfree.chart.JFreeChart
 @ line 4, column 1.
   import org.jfree.chart.JFreeChart
   ^

4 errors

I have copied the JCommon and all the JFreeChart libraries into the lib folder. And I still see this error. Please help. I am very new to Groovy and Grails.


Solution

  • Steps to add your code:

    1. Add your jar dependency which will download it in the BuildConfig.groovy (you need to find how to download it using maven repository by searching online your jar version over the web) http://mvnrepository.com/artifact/jfree/jfreechart
    2. Run "grails clean"
    3. Run "grails refresh-dependencies"