Question

I'm trying yo use JFreeChart in RCP application with a simple view. I added JfreeChart jar file as a referenced library to project.

The view could not be created because of the error :

Could not create the view: Plug-in "ViewJFreeChart" was unable to instantiate class "viewJFreeChart.View".
java.lang.NoClassDefFoundError: org/jfree/data/general/PieDataset
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getConstructor0(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)

......

This is the source code for View.java:

package viewJFreeChart;

import java.awt.Font;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public class View extends ViewPart {
    public static final String ID = "ViewJFreeChart.view";

    private TableViewer viewer;

    /**
     * This is a callback that will allow us to create the viewer and initialize
     * it.
     */
    public void createPartControl(Composite parent) {

        JFreeChart chart = createChart(createDataset());
        final ChartComposite frame = new ChartComposite(parent, SWT.NONE,
                chart, true);
    }

    /**
     * Passing the focus request to the viewer's control.
     */
    public void setFocus() {
        viewer.getControl().setFocus();
    }

    /**
     * Creates the Dataset for the Pie chart
     */
    private PieDataset createDataset() {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("One", new Double(43.2));
        dataset.setValue("Two", new Double(10.0));
        dataset.setValue("Three", new Double(27.5));
        dataset.setValue("Four", new Double(17.5));
        dataset.setValue("Five", new Double(11.0));
        dataset.setValue("Six", new Double(19.4));
        return dataset;
    }

    /**
     * Creates the Chart based on a dataset
     */
    private JFreeChart createChart(PieDataset dataset) {

        JFreeChart chart = ChartFactory.createPieChart("Pie Chart Demo 1", // chart
                // title
                dataset, // data
                true, // include legend
                true, false);

        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setSectionOutlinesVisible(false);
        plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        plot.setNoDataMessage("No data available");
        plot.setCircular(false);
        plot.setLabelGap(0.02);
        return chart;

    }
}
Was it helpful?

Solution

When you say you added the dependency as a referenced library, I assume you mean you added it as a jar dependency to the Eclipse project via the Java Build Path section of the project properties, this will work only for compilation in your workspace. RCP needs dependencies/nested jars to be defined in the Manifest so they are resolved in the target platform.

To set a plugin dependency, open up your Manifest file, you should see a multi-tabbed editor. Select the Dependencies tab and click Add..., select the plugin, and OK.

dependencies tab screenshot

If you are choosing to bundle the jar in your plugin, you instead need to add it to the Manifest's classpath. Assuming the jar is in a sub-directory of your plugin project, select the Runtime tab of the Manifest editor, then select the Classpath section's Add... button. Add the jar and OK.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top