使用poi时,两个环境下,一个错误一直正常

错误信息如下:

[2018-10-15 12:09:14] [WARNING] [System.out] [Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:162)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:142)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:37)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:65)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.openxml4j.opc.OPCPackage.create(OPCPackage.java:338)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.xssf.usermodel.XSSFWorkbook.newPackage(XSSFWorkbook.java:434)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:218)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:216)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:185)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:160)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at org.apache.poi.xssf.streaming.SXSSFWorkbook.<init>(SXSSFWorkbook.java:255)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ at com.loncom.samc.servlet.ExportAction.exportalarm(ExportAction.java:31)]
[2018-10-15 12:09:14] [WARNING] [System.out] [ ... 39 more]

Caused by: java.lang.ClassNotFoundException: org.apache.poi.xssf.streaming.SXSSFWorkbook

Caused by: javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.EventFactory not found

 

通过下面的类,找两个环境下,看加载类是从哪个包来的,发现我的是webservice-api.jar冲突造成的。

public class ClassUtil {

    public static List<String> getClassLocation(List<Class> listClass){
        List<String> list=new ArrayList<String>();
        for (Class class1 : listClass) {
            list.add(""+getClassLocation(class1));
        }
        return list;
    }
    public static URL getClassLocation(final Class cls) {
        if (cls == null)
            throw new IllegalArgumentException("null input: cls");
        URL result = null;
        final String clsAsResource = cls.getName().replace('.', '/').concat(".class");
        final ProtectionDomain pd = cls.getProtectionDomain();
        // java.lang.Class contract does not specify if 'pd' can ever be null;
        // it is not the case for Sun's implementations, but guard against null
        // just in case:
        if (pd != null) {
            final CodeSource cs = pd.getCodeSource();
            // 'cs' can be null depending on the classloader behavior:
            if (cs != null)
                result = cs.getLocation();
            if (result != null) {
                // Convert a code source location into a full class file
                // location
                // for some common cases:
                if ("file".equals(result.getProtocol())) {
                    try {
                        if (result.toExternalForm().endsWith(".jar")|| result.toExternalForm().endsWith(".zip"))
                            result = new URL("jar:".concat(result.toExternalForm())
                                    .concat("!/").concat(clsAsResource));
                        else if (new File(result.getFile()).isDirectory())
                            result = new URL(result, clsAsResource);
                    } catch (MalformedURLException ignore) {
                        ignore.printStackTrace();
                    }
                }
            }
        }
        if (result == null) {
            // Try to find 'cls' definition as a resource; this is not
            // document.d to be legal, but Sun's implementations seem to //allow
            // this:
            final ClassLoader clsLoader = cls.getClassLoader();
            result = clsLoader != null ? clsLoader.getResource(clsAsResource)
                    : ClassLoader.getSystemResource(clsAsResource);
        }
        return result;
    }
    
    public static void main(String[] args) {
        String  classLocation =  ""+getClassLocation(ZipPackagePropertiesMarshaller.class);
        System.out.println(classLocation);
        
        List<Class> l=new ArrayList<Class>();
        l.add(ZipPackagePropertiesMarshaller.class);
        l.add(SXSSFWorkbook.class);
        System.out.println(getClassLocation(l));
    }
}

 

posted @ 2018-10-17 16:15  magina_敌法  阅读(7164)  评论(0编辑  收藏  举报