java.lang.ClassCastException:com.ibm.ws.classloader.Handler $ ClassLoaderURLConnection与java.net.JarURLConnection不兼容

时间:2013-04-05 20:59:32

标签: java jar resources websphere

如何使用wsjar文件列出目录的内容?

下面的代码不适用于WebSphere,但适用于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( ) );
        }
    }
}

在WebSphere上,我得到了:

java.lang.ClassCastException:com.ibm.ws.classloader.Handler $ ClassLoaderURLConnection与java.net.JarURLConnection不兼容

1 个答案:

答案 0 :(得分:0)

您可以尝试通过从网址中剥离jar:file:并使用ZipFile type来尝试打开,因为您并不真正对jar作为jar感兴趣,而是将其中的文件jar(只是一个zip文件)。

因此,"jar:file:///foo/bar/baz.jar"变为"/foo/bar/baz.jar",您传递给ZipFile(String) constructor