2009-10-02 178 views
6

我想喲在RCP應用程序中使用JFreeChart與簡單的看法。我將JfreeChart jar文件添加爲項目引用的庫。Eclipse RCP與JFreeChart

視圖不能因爲錯誤的創建:

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) 

......

這是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; 

    } 
} 

回答

7

你說你添加了依賴項作爲引用庫,我假定你的意思是你添加它作爲一個jar依賴項到Eclipse項目通過Java Build Path部分e項目屬性,這將僅適用於工作區中的編譯。 RCP需要在Manifest中定義依賴關係/嵌套罐,以便在目標平臺中解析它們。

要設置插件依賴項,請打開您的Manifest文件,您應該看到一個多選項卡式編輯器。 選擇依賴項選項卡並單擊添加...,選擇插件,然後單擊確定。

dependencies tab screenshot

如果您選擇捆綁在插件的罐子,你反而需要將其添加到清單的類路徑。假設該jar文件位於插件項目的子目錄中,請選擇Manifest編輯器的運行時選項卡,然後選擇類路徑節的添加...按鈕。添加jar並確定。

+0

謝謝。它將JFreeChart庫添加到Manifest的類路徑後起作用。 – penguru 2009-10-02 08:55:02

+0

不客氣,欲瞭解更多信息,請參閱有關RCP應用程序的3部分教程:http://www.eclipse.org/articles/Article-RCP-1/ tutorial1.html – 2009-10-02 09:22:17

+0

很好地放和說明,像往常一樣。 +1 – VonC 2009-10-02 10:47:56