1

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?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I think this link can be useful: https://stackoverflow.com/questions/58084891/how-to-add-jfreechart-library-to-jdk-error-package-org-jfree-chart-does-not-ex –  Jul 20 '21 at 13:25
  • The comment on that post is super difficult to understand. – Saarth Kurle Jul 20 '21 at 13:33
  • As an aside: See also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Jul 20 '21 at 16:30
  • Nevermind I solved it I just had to compile it as "javac -cp kk/jfreechart.jar:kk/jcommon.jar:. A1Test.java" – Saarth Kurle Jul 20 '21 at 17:26
  • For reference, the current release, [v 1.5.3](https://mvnrepository.com/artifact/org.jfree/jfreechart/1.5.3) is a single JAR. – trashgod Jul 20 '21 at 22:16

2 Answers2

1

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.

0

Well I just copy pasted your code on my Mac. It works absolutely fine. I ran the following commands:

javac -classpath kk/"*":./ A1Test.java 
java -classpath kk/"*":./ A1Test 

This produces the following screen:

enter image description here

  • I use / for a path (Windows uses \)
  • I run it from the directory that contains A1Test.java
  • kk directory and A1Test.java are siblings in the directory tree structure
  • I append current directory to the class path. Not doing so will result in A1Test.java not being found and throw java.lang.ClassNotFoundException.
  • I also escaped the wildcard asterisk in double quotes.
Wasim Zarin
  • 166
  • 3
  • 1
    Quoting the `*` in this context does nothing; I suspect that the [tag:jfreechart] JAR(s) are elsewhere on your classpath. perhaps one of the system paths. – trashgod Jul 20 '21 at 18:38
  • Thanks for your answer. I compiled it with "javac -cp kk/jfreechart.jar:kk/jcommon.jar:. A1Test.java" and it works. I am sure your answer works fine as well. – Saarth Kurle Jul 21 '21 at 17:53