Search code examples
javachartsjfreechart

Error while making a chart in Java on MacOS


I am working on a simple test code to make bar charts in java. This is my code:

import javax.swing.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.*;

class A1 extends JFrame{
A1(){
    // s1-> dataset
    DefaultCategoryDataset ds = new DefaultCategoryDataset();
    ds.addValue(89, "Amit", "Phy");
    ds.addValue(50, "Amit", "Chem");
    ds.addValue(78, "Amit", "Math");

    //s2-> design chart
    JFreeChart ch = ChartFactory.createBarChart("Amit's performance", "Subject", "Marks", 
        ds, PlotOrientation.VERTICAL, true, true, false);

    //s3-> display chart
    ChartPanel cp = new ChartPanel(ch);
    setContentPane(cp);

    setTitle("My First Chart App");
    setSize(500, 400);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
 }
 }

 class A1Test{
     public static void main(String[] args) {
         A1 a = new A1();
    }
 } 

I have jcommon.jar and jfreechart.jar in the folder kk.

I ran it with the command "javac -classpath kk\* A1test.java" on terminal and I always get the error "java:2: error: package org.jfree.chart does not exist"

Can anyone please help me solve this?


Solution

  • So I ran it with the following terminal command

    javac -cp kk/jfreechart.jar:kk/jcommon.jar:. A1Test.java
    

    And now it works completely fine.