Thursday

(?i) and (?-i) in regex means

(?i) turns on case-insensitive mode, (?-i) turns it off.

For example:
For regex (?i)ed(?-i)a 
 - matches with the following words: eda, Eda, eDa, EDa
 - does not match with the following words: edA, EdA, eDA, EDA

Wednesday

java.io.InvalidClassException: com.oracle.pitchfork.interfaces.LifecycleCallbackException; local class incompatible:

BUG:
java.io.InvalidClassException: com.oracle.pitchfork.interfaces.LifecycleCallbackException; local class incompatible:

FIX:
Define a serialVerionUID in your class:
private static final long serialVersionUID = 6529685098267757690L;

Thursday

Upload and parse excel file - JSP

1. Download and add the necessary libraries to the classpath.
  • poi-3.9.jar
  • poi-ooxml-3.9.jar
  • commons-fileupload-1.3.jar
  • poi-ooxml-schemas-3.9.jar
  • dom4j-1.6.1.jar
2. You should add to the .jsp file the following codes:
 <form action="/XXX/TestServlet" method="post" name="testForm" id="testForm" enctype="multipart/form-data" >
<input type="file" name="mptest" >
<input type="submit" value="Yükle">
</form>

3.TestServlet.java:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class TestServlet extends HttpServlet {

    public TestServlet() {
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }

    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
           if(ServletFileUpload.isMultipartContent(request)) {
             try {

                List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);

                if(multiparts != null && multiparts.size() > 0) {
                     FileItem item = multiparts.get(0);

                    // control file mimetype
                    if(item.getContentType().equals("application/vnd.ms-excel") || item.getContentType().equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {

                          if(!item.isFormField()) {
                               InputStream in = item.getInputStream();
                               Workbook wb = WorkbookFactory.create(in);

                               Sheet mySheet = wb.getSheetAt(0);// get first  sheet
                               Iterator<Row> rowIter = mySheet.rowIterator();// get row list

                              while(rowIter.hasNext()) {
                                   List<String> cellList = new ArrayList<String>();
        
                                   Row row = rowIter.next();
                                   Iterator<Cell> cellIterator = ((Row) row).cellIterator();// get cell list

                                   // get all cell as String
                                  while(cellIterator.hasNext()) {
                                      Cell cell = cellIterator.next();
     
                                      switch(cell.getCellType()) {
                                          case Cell.CELL_TYPE_BOOLEAN:
                                              cellList.add(Boolean.toString(cell.getBooleanCellValue()));
                                              break;
        
                                          case Cell.CELL_TYPE_NUMERIC:
                                               cellList.add(String.valueOf(cell.getNumericCellValue()));
                                               break;
        
                                         case Cell.CELL_TYPE_STRING:
                                                cellList.add(cell.getStringCellValue());
                                                break;
        
                                        case Cell.CELL_TYPE_BLANK:
                                              break;
                                    }
     
                               }
                              // do your job for the 'row' and 'cellList'
                           }
                        }
       
                    } else {
                     // the file is not an excel file
                  }
               }
      
            } catch(Exception e) {
              // upload error
            }
  
        } else {
           // wrong file error message
        }
     
     } catch(Exception e) {
        // exception
      }
 
  }

}

Tuesday

How to solve JXL error : jxl.read.biff.BiffException: Unable to recognize OLE stream

BUG:
I got following error while i was reading the uploaded excel file (.xlsx) by using jxl library.
how to solve JXL error : jxl.read.biff.BiffException: Unable to recognize OLE stream

FIX:
JXL library does not support .xslx format, you should use Apache POI (WorkbookFactory) instead of JXL.(poi.jar)

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML.

BUG:
While i was trying to read uploaded excel by apache poi (HSSFWorkbook) i got following error:

Error:  org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML.
 You are calling the part of POI that deals with OLE2 Office Documents. 
 You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

FIX:
The error is taken only with the older .xls (binary) files. 
For both .xls and .xlsx files you can use WorkbookFactory. Download poi-ooxml-3.9.jar and add it to classpath.

Workbook workbook = WorkbookFactory.create(new FileInputStream("E:/Code.xlsx"));
Sheet firstSheet = workbook .getSheetAt(0);
Iterator<Row> rowIter = firstSheet.rowIterator(); 
while(rowIter.hasNext()) {
Object row = rowIter.next();
       Iterator<Cell> cellIterator = ((Row) row).cellIterator();

 }

java.lang.NoClassDefFoundError: org/dom4j/Namespace at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller

BUG:
While i was trying to read uploaded excel by apache poi i got following error:
java.lang.NoClassDefFoundError: org/dom4j/Namespace

at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.<clinit>(PackagePropertiesUnmarshaller.java:49)

FIX:
I downloaded dom4j.jar and add it to classpath.
You can download the jar from here.

Root cause of ServletException.java.lang.NoClassDefFoundError: org/openxmlformats/schemas/drawingml/x2006/main/ThemeDocument

BUG:
While i was trying to read uploaded excel by apache poi i got following error:
   Workbook wb = WorkbookFactory.create(new FileInputStream(filename));

 Error:    Root cause of ServletException.java.lang.NoClassDefFoundError:           org/openxmlformats/schemas/drawingml/x2006/main/ThemeDocument

FIX:
I downloaded poi-ooxml-schemas.jar and add it to classpath.
You can download the jar from here.

Parsing wsdl error: soapenc:Array' to a(n) 'type definition

BUG:   While I was parsing wsdl file by using wsimport command (JAX-WS), I got this error: soapenc:Array' to a(n) 'type definition

FIX:   soapenc:Array is 'RPC-Encoding' web service style. Apache Axis 1 supports RPC encoded so parse the wsdl by Apache Axis 1.

You can use Soap-UI for parsing:
     Tools >> Axis1 Artifacts >> (write wsdl and output directory) >> Generate

java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 2

BUG:
I have the following String:
           String keyword = '\AMore\s*Code\Z';
and i want to replace regexs as following:
             keyword.replace("[\\A", "").replace("[\\Z", "").replace("[\\s*", " ") : "";
But i got the following error:
java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 2

FIX:
Use the java.util.regex.Pattern to escape special characters in regex:
  keyword = Pattern.compile("\\\\A").matcher(keyword).replaceAll(" ");



Thursday

Eclipse error: 'failed to create java virtual machine'

BUG:
I got this error message when i started Eclipse indigo: 'failed to create java virtual machine'

FIX:
Things that worked for me:
1.Open eclipse.ini file
2.Remove -vmargs
3.Add -vm C:\Program Files\Java\jre7\bin\javaw
4. Update --launcher.XXMaxPermSize to 256M