从jar获取已打包文件(具体参考hibernate3的源代码)

1.创建jar对象
java.util.jar.JarFile jarFile;
java.io.File jar;

2.获取对象
try {
    jarFile = new JarFile( jar );
}
catch ( IOException ioe ) {
    log.error( "Could not configure datastore from jar: " + jar.getName(), ioe );
    throw new MappingException( "Could not configure datastore from jar: " + jar.getName(), ioe );
}

3.获取jar对象内的压缩文件对象
Enumeration jarEntries = jarFile.entries();

4.使用对象
while ( jarEntries.hasMoreElements() ) {
 ZipEntry ze = ( ZipEntry ) jarEntries.nextElement();
 if ( ze.getName().endsWith( ".hbm.xml" ) ) {
  log.info( "Found mapping documents in jar: " + ze.getName() );
  try {
   addInputStream( jarFile.getInputStream( ze ) );  // 重点-使用对象
   }
  catch ( MappingException me ) {
  throw me;
  }
  catch ( Exception e ) {
  log.error( "Could not configure datastore from jar: " + jar.getName(), e );
  throw new MappingException( "Could not configure datastore from jar: " + jar.getName(), e );
  }
 }
}

你可能感兴趣的:(exception,Hibernate,jar)