简体   繁体   中英

java.lang.ClassCastException: com.ibm.ws.classloader.Handler$ClassLoaderURLConnection incompatible with java.net.JarURLConnection

How do i list the contents of a directory with wsjar files?

The code below is not working for WebSphere but does work for JBoss:

Enumeration<URL> en = getClass( ).getClassLoader( ).getResources( "com/myjars" );

while( en.hasMoreElements( ) ) {

    URL url = en.nextElement( );

    if( url.toString( ).startsWith( "jar:file:" ) || url.toString( ).startsWith( "wsjar:file:" ) ) {

        JarFile jarFile = ( (JarURLConnection)url.openConnection( ) ).getJarFile( );

        Enumeration<JarEntry> eje = jarFile.entries( );

        while( eje.hasMoreElements( ) ) {
            JarEntry jarEntry = eje.nextElement( );

            System.out.println( jarEntry.getName( ) );
        }
    }
}

On WebSphere, i got:

java.lang.ClassCastException: com.ibm.ws.classloader.Handler$ClassLoaderURLConnection incompatible with java.net.JarURLConnection

You might try opening by stripping the jar:file: from the URL and using a ZipFile type instead, since you're not really interested in the jar as a jar, but rather the files within the jar (which is just a zip file).

So, "jar:file:///foo/bar/baz.jar" becomes "/foo/bar/baz.jar" which you pass to the ZipFile(String) constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM