Eclipse RCP with JFreeChart
Asked Answered
D

1

6

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;

    }
}
Diacritic answered 2/10, 2009 at 7:49 Comment(0)
P
7

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.

Pomegranate answered 2/10, 2009 at 8:30 Comment(5)
Thank you. It works after adding the JFreeChart libraries to the Manifest's classpath..Diacritic
You're welcome, for more information see this 3-part tutorial on RCP apps: eclipse.org/articles/Article-RCP-1/tutorial1.htmlPomegranate
Nicely put and illustrated, as usual. +1Digital
@Rich Seller "Assuming the jar is in a sub-directory of your plugin project".. if I want add the jar files to run time tab of more than one plugin, where should I store them instead of the sub-directory of plugin ?Diacritic
You can host the jar in one plugin and add that plugin as a dependency of others. You'd need to ensure that the JFreeChart jar's packages are exported in the plugin's manifest (under the Runtime tab)Pomegranate

© 2022 - 2024 — McMap. All rights reserved.