[Gvsig_desarrolladores] Error grave de la aplicación. Es conveniente que salgas de la aplicación. java.lang.NoClassDefFoundError: jxl/write/WritableCell

Antonio Araque antonio.araque en e-sig.es
Mie Ene 25 12:19:04 CET 2012


Hola, estoy intentando reutilizar la extensión extTableExport, para exportar
la información alfanumérica de una capa a un Excel. 

 

Me he creado una clase con los métodos que necesito:

 

import java.awt.Component;

import java.io.File;

import java.util.Iterator;

import java.util.NoSuchElementException;

 

import javax.swing.JFileChooser;

import javax.swing.JOptionPane;

 

import jxl.JXLException;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.Number;

import jxl.write.NumberFormats;

import jxl.write.WritableCell;

import jxl.write.WritableCellFormat;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import com.hardcode.gdbms.driver.exceptions.ReadDriverException;

import com.hardcode.gdbms.engine.data.driver.DriverException;

import com.hardcode.gdbms.engine.values.DoubleValue;

import com.hardcode.gdbms.engine.values.FloatValue;

import com.hardcode.gdbms.engine.values.NullValue;

import com.hardcode.gdbms.engine.values.NumericValue;

import com.hardcode.gdbms.engine.values.Value;

import com.iver.andami.PluginServices;

import com.iver.cit.gvsig.fmap.drivers.FieldDescription;

import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;

import com.iver.cit.gvsig.fmap.layers.FBitSet;

import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;

import com.iver.cit.gvsig.project.documents.table.gui.Table;

import com.iver.utiles.GenericFileFilter;

 

public class ExportarExcel {

      

      private WritableCellFormat floatFormat = new WritableCellFormat
(NumberFormats.FLOAT);

      private WritableCellFormat intFormat = new WritableCellFormat
(NumberFormats.INTEGER);

      

      public ExportarExcel(){

            

      }

 

      public void exportToXLS(Table table) throws Exception{

            File file = this.askForFileName("xls","Excel");

            if (file == null){

                  return;

            }

            exportToFile(table,file);

      }

 

      public void exportToFile(Table table,File trgfile) throws Exception{

            SelectableDataSource source;

            ITableDefinition orgDef;

            try {

                  source = table.getModel().getModelo().getRecordset();

                  source.start();

                  orgDef =
table.getModel().getModelo().getTableDefinition();

            } catch (Exception e) {

                  throw new Exception("Error preparando la fuente", e); //
TODO intenacionalizacion??

            }

 

 

            File file = new File(trgfile.getAbsoluteFile()+".tmp");

 

            WritableWorkbook workbook = Workbook.createWorkbook(file);

            WritableSheet sheet = workbook.createSheet("First Sheet", 0);

 

            writeHeader(sheet,orgDef);

 

 

            try {

                  SourceIterator iter = new SourceIterator(source);

                  Value[] values;

                  Value value;

                  int row=1;

                  int col;

                  while (iter.hasNext()){

                         values = iter.nextValues();

                         for (col=0;col<values.length;col++){

                             value = values[col];

                             if (!(value instanceof NullValue)){

                                   sheet.addCell(getCell(row,col,value));

                             }

                         }

                         row++;

 

                  }

 

                   workbook.write();

 

                   workbook.close();

 

            } catch (Exception e) {

                  throw new Exception("Error generando fichero", e); // TODO
intenacionalizacion??

 

            }

 

            file.renameTo(trgfile);

 

            try {

                  source.stop();

 

            } catch (Exception e) {

                  throw new Exception("Error cerrando ficheros", e); // TODO
intenacionalizacion??

            }

 

 

 

      }

      

      private File askForFileName(String ext,String extDescription){

            JFileChooser jfc = new JFileChooser();

            jfc.addChoosableFileFilter(new GenericFileFilter(ext,

                        extDescription));

            if (jfc.showSaveDialog((Component)
PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {

                  File file=jfc.getSelectedFile();

                  if (file == null){

                        return null;

                  }

                  if (file.exists()){

                        int resp = JOptionPane.showConfirmDialog(

                                   (Component)
PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe
_seguro_desea_guardarlo"),

                                   PluginServices.getText(this,"guardar"),
JOptionPane.YES_NO_OPTION);

                        if (resp != JOptionPane.YES_OPTION) {

                             return null;

                        }

                  }

                  String name = file.getAbsolutePath();

                  if (!name.toLowerCase().endsWith("." +ext.toLowerCase())){

                        file = new File(name + "."+ext);

                  }

                  return file;

            } else{

                  return null;

            }

 

      }

      

      private WritableCell getCell(int row, int col, Value value) {

            if (value instanceof NumericValue){

                  if (value instanceof DoubleValue || value instanceof
FloatValue){

                        return new
Number(col,row,((NumericValue)value).doubleValue(),floatFormat);

                  } else {

                        return new
Number(col,row,((NumericValue)value).longValue(),intFormat);

                  }

            } else{

                  return new Label(col,row,value.toString());

            }

 

      }

      

      private void writeHeader(WritableSheet sheet, ITableDefinition orgDef)
throws JXLException {

            FieldDescription[] fields = orgDef.getFieldsDesc();

            FieldDescription field;

            Label cell;

            int col;

            for (col=0;col<fields.length;col++){

                  field = fields[col];

                  cell=new Label(col,0,field.getFieldName());

                  sheet.addCell(cell);

            }

      }

      

      private class SourceIterator implements Iterator{

      private int index;

      private long count;

      private FBitSet selection = null;

      private SelectableDataSource source;

 

      public SourceIterator(SelectableDataSource source) throws
DriverException, ReadDriverException{

            this.source = source;

            if (source.getSelection().cardinality()== 0){

                  this.selection = null;

                  this.index=0;

                  this.count = source.getRowCount();

            } else{

                  this.selection = source.getSelection();

                  this.index=selection.nextSetBit(0);

                  this.count = selection.cardinality();

            }

 

      }

 

            public void remove() {

                  throw new UnsupportedOperationException();

            }

 

            public boolean hasNext() {

                  if (this.selection == null)

                        return this.index < this.count;

                  return this.index >= 0;

            }

 

            public Object next() {

                  try {

                        if (!hasNext()){

                             throw new NoSuchElementException();

                        }

                        return this.nextValues();

                  } catch (DriverException e) {

                        throw new RuntimeException(e);

                  } catch (ReadDriverException e) {

                        throw new RuntimeException(e);

                  }

            }

 

            public Value[] nextValues() throws DriverException,
ReadDriverException {

 

                  Value[] values = this.source.getRow(this.index);

                  if (this.selection == null){

                        this.index++;

                  } else {

                        this.index = this.selection.nextSetBit(this.index +
1);

                  }

                  return values;

 

            }

 

            public long count(){

                  return this.count;

            }

 

    }

 

 

}

 

 

He importado al build path de mi extensión la librería jxl-2.6.6.jar
proveniente de extTableExport.

 

El problema es que cuando llamo al constructor de la clase ExportarExcel, me
aparece el siguiente error:

 

 

WARN [AWT-EventQueue-1] (PluginClassLoader.java:191) - PluginLoaders[i] es
nulo

 WARN [AWT-EventQueue-1] (PluginClassLoader.java:191) - PluginLoaders[i] es
nulo

DEBUG [AWT-EventQueue-1] (NotificationManager.java:104) - Error grave de la
aplicación. Es conveniente que salgas de la aplicación.

java.lang.NoClassDefFoundError: jxl/write/WritableCell

      at
com.iver.cit.gvsig.myplugin.panels.PanelFiltro$FuncionalidadBoton.exportarXL
S(PanelFiltro.java:754)

      at
com.iver.cit.gvsig.myplugin.panels.PanelFiltro$FuncionalidadBoton.actionPerf
ormed(PanelFiltro.java:625)

      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown
Source)

      at java.awt.Component.processMouseEvent(Unknown Source)

      at javax.swing.JComponent.processMouseEvent(Unknown Source)

      at java.awt.Component.processEvent(Unknown Source)

      at java.awt.Container.processEvent(Unknown Source)

      at java.awt.Component.dispatchEventImpl(Unknown Source)

      at java.awt.Container.dispatchEventImpl(Unknown Source)

      at java.awt.Component.dispatchEvent(Unknown Source)

      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

      at java.awt.Container.dispatchEventImpl(Unknown Source)

      at java.awt.Window.dispatchEventImpl(Unknown Source)

      at java.awt.Component.dispatchEvent(Unknown Source)

      at java.awt.EventQueue.dispatchEvent(Unknown Source)

      at
com.iver.andami.ui.AndamiEventQueue.dispatchEvent(AndamiEventQueue.java:58)

      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

      at java.awt.EventDispatchThread.run(Unknown Source)

Caused by: java.lang.ClassNotFoundException: Error leyendo
fichero:jxl.write.WritableCell

      at
com.iver.andami.plugins.PluginClassLoader.loadClass(PluginClassLoader.java:2
51)

      at java.lang.ClassLoader.loadClass(Unknown Source)

      ... 28 more       

 

Me está dando el error de que no encuentra la clase jxl.write.WritableCell,
pero no entiendo el porqué del error, porque como he comentado anteriormente
tengo incluido en el build Path el jar que contiene la clase WritableCell en
jxl.write.

 

Por más vueltas que le doy, no consigo solucionarlo. Alguien sabe a qué
puede ser debido??

 

Gracias.

 

Un saludo.

 

Antonio Araque.

 

------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: http://listserv.gva.es/pipermail/gvsig_desarrolladores/attachments/20120125/5e8d2ed9/attachment.htm 


Más información sobre la lista de distribución gvSIG_desarrolladores