Can´t load Apache POI

Issue #11 closed
Raphael Milani
created an issue

Hi I´ve been trying to use jxls with POI, but the API can´t load the apache-poi see the error below. I have on my classpath poi-3.17.jar the last version. My pom is:

<dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls</artifactId>
            <version>2.4.3</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-collections</artifactId>
                    <groupId>commons-collections</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.jxls</groupId>
            <artifactId>jxls-poi</artifactId>
            <version>1.0.14</version>
            <!-- <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
            </exclusions> -->
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <!-- ojdbc7.jar example -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc8</artifactId>
            <version>12.2.0.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.jxls.util.TransformerFactory.loadPoiTransformer(TransformerFactory.java:85)
    at org.jxls.util.TransformerFactory.getTransformerClass(TransformerFactory.java:69)
    at org.jxls.util.TransformerFactory.createTransformer(TransformerFactory.java:27)
    at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:379)
    at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:173)
    at br.com.votorantim.report.PisCofinsExcel.criarRelatorio(PisCofinsExcel.java:124)
    at br.com.votorantim.report.PisCofinsExcel.main(PisCofinsExcel.java:186)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 9 more

Comments (11)

  1. Raphael Milani reporter

    Hi @Leonid Vysochyn I tried without POI dependency because jxls-poi v1.0.14 provided as you said. I looked the demo and POM´s file contains this:

     <!--<systemProperties>-->
                            <!--<systemProperty>-->
                                <!--<key>jxlstransformer</key>-->
                                <!--<value>poi</value>-->
                            <!--</systemProperty>-->
                        <!--</systemProperties>-->
    

    Does it necessary ?

  2. Raphael Milani reporter

    Hi @Leonid Vysochyn

    I tried to run the Demo and I catch the same error(org.jxls.demo.SqlDemo):

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at org.jxls.util.TransformerFactory.loadPoiTransformer(TransformerFactory.java:85)
        at org.jxls.util.TransformerFactory.getTransformerClass(TransformerFactory.java:69)
        at org.jxls.util.TransformerFactory.createTransformer(TransformerFactory.java:27)
        at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:379)
        at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:173)
        at org.jxls.demo.SqlDemo.main(SqlDemo.java:45)
    Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 8 more
    
  3. Leonid Vysochyn repo owner

    Right, I have noticed that the latest changes to jxls-demo project to work with jxls-2.4.3 release has not been pushed to repo. So now it's fixed and it should work fine (it works for me).

    Step 1. Get the latest code from master:

    git clone https://bitbucket.org/leonate/jxls-demo
    

    Step 2. Run all the examples in jxls-demo folder

    mvn clean install
    

    Does this work for you?

  4. Raphael Milani reporter

    Hi I´ve just updated the code with the last version and I ran maven again, take a look:

    [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jxls-demo ---
    [INFO] Building jar: C:\kit\workspaces\java\votorantim\jxls-demo\target\jxls-demo-1.0.0-SNAPSHOT.jar
    [INFO] 
    [INFO] >>> exec-maven-plugin:1.2.1:java (default) > validate @ jxls-demo >>>
    [INFO] 
    [INFO] <<< exec-maven-plugin:1.2.1:java (default) < validate @ jxls-demo <<<
    [INFO] 
    [INFO] 
    [INFO] --- exec-maven-plugin:1.2.1:java (default) @ jxls-demo ---
    INFO org.jxls.demo.guide.ObjectCollectionDemo - Running Object Collection demo
    INFO org.jxls.template.SimpleExporter - you may change the SPI on file: META-INF/services/org.jxls.util.ServiceFactory
    INFO org.jxls.template.SimpleExporter - you may change the SPI on file: META-INF/services/org.jxls.util.JxlsConfigProvider
    [WARNING] 
    java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at org.jxls.util.TransformerFactory.loadPoiTransformer(TransformerFactory.java:85)
        at org.jxls.util.TransformerFactory.getTransformerClass(TransformerFactory.java:69)
        at org.jxls.util.TransformerFactory.createTransformer(TransformerFactory.java:27)
        at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:379)
        at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:173)
        at org.jxls.demo.guide.ObjectCollectionDemo.main(ObjectCollectionDemo.java:32)
        at org.jxls.demo.MainDemo.main(MainDemo.java:23)
        ... 6 more
    Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 15 more
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 9.363 s
    [INFO] Finished at: 2018-02-01T16:39:17-02:00
    [INFO] Final Memory: 22M/151M
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (default) on project jxls-demo: An exception occured while executing the Java class. null: InvocationTargetException: org/apache/poi/ss/usermodel/Workbook: org.apache.poi.ss.usermodel.Workbook -> [Help 1]
    

    There is some conflict with libraries ...

  5. Leonid Vysochyn repo owner

    Right, this is strange that it fails for you but works for me (tried on 2 different machines both Mac and Windows with a fresh clone of jxls-demo from the GitHub repo). Could you please share what are JDK and Maven version you use and what is your OS ?

  6. Leonid Vysochyn repo owner

    Overall it looks like you have some issues with POI libraries in class path. Although this should be managed by maven but can you check that you don't have any global classpath defined? To force the POI transformer you can set Java system property "jxlstransformer" to "poi":

    -Djxlstransformer=poi
    

    If the property not defined the TransformerFactory will first try to load the POI Transformer from class path and if it fails then Jexcel Transformer. The responsible code is

       public static final String TRANSFORMER_SYSTEM_PROPERTY = "jxlstransformer";
    
        private static Class getTransformerClass() {
            String transformerName = System.getProperty(TRANSFORMER_SYSTEM_PROPERTY);
            Class transformer = null;
            if( transformerName == null ){
                transformer = loadPoiTransformer();
                if (transformer == null) {
                    transformer = loadJexcelTransformer();
                }
            }else{
                if( POI_TRANSFORMER.equalsIgnoreCase(transformerName ) ){
                    transformer = loadPoiTransformer();
                }else if( JEXCEL_TRANSFORMER.equalsIgnoreCase(transformerName)){
                    transformer = loadJexcelTransformer();
                }
            }
            return transformer;
        }
    

    However in your case it definitely looks like some library conflict or missing some POI dependencies

  7. Log in to comment