skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, October 10, 2012

Exploring Jar Contents

Posted by Raju Gupta at 12:27 PM – 0 comments
 
This solution contains code through which you can first reach a particular directory whose name has to be provided and that should exist.
In that directory you can shorlist the files with extension .jar.Next you need to explore the contents of the Jar file.Here in this solution the jar contains images
and we get the list of files and add them to a java.util.List object.
import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


public class ExploreJarContent {
 
 public static void main(String[] args) throws IOException {
  ExploreJarContent exploreJar = new ExploreJarContent();
  List<String> resourceList = new ArrayList<String>();
  String resourceName = "/images/";
  exploreJar.loadResourceJar(resourceName, resourceList);
 }

 /**
  * @param resourceName
  * @param resourceList
  */
 private void loadResourceJar(String resourceName, List<String> resourceList) {

  URL url = this.getClass().getResource(resourceName);
  String[] jarName = null;
  System.out.println("-----url--- " + url);
  int jarNameIndex = 0;
  StringBuffer jarUrl;
  // To get only the jar file
  String strFile;
  if (null != url) {
   strFile = url.getFile();
   if (null != strFile) {
    File file = new File(strFile);
    File[] listOfFiles = file.listFiles();
    jarName = new String[listOfFiles.length];
    for (int i = 0; i < listOfFiles.length; i++) {
     if (listOfFiles[i].isFile()) {
      if (listOfFiles[i].getName().endsWith((".jar"))) {
       jarName[jarNameIndex] = listOfFiles[i].getName();
       jarNameIndex++;
      }
     }
    }
   }
  }
  if (null != jarName) {
   for (int i = 0; i < jarName.length; i++) {
    if (null == jarName[i])
     break;
    jarUrl = new StringBuffer();
    jarUrl.append("jar:").append(url).append(jarName[i]).append(
      "!/");
    URL url_jar = null;

    try {
     url_jar = new URL(jarUrl.toString());
    } catch (MalformedURLException e1) {
     e1.printStackTrace();
    }

    try {
     if (url_jar.openConnection() instanceof JarURLConnection) {
      JarURLConnection jarUrlConn = (JarURLConnection) url_jar
        .openConnection();
      if (jarUrlConn != null) {
       JarFile jar = jarUrlConn.getJarFile();
       Enumeration enm = jar.entries();

       while (enm.hasMoreElements()) {
        JarEntry entry = (JarEntry) enm.nextElement();
        if (!entry.isDirectory()) {
         String entryName = entry.getName();

         resourceList.add("/" + entryName);

         System.out.println("----entry Name---"
           + entryName);
        }
       }

      }
     }
    } catch (IOException e) {
     e.printStackTrace();
    }
    // }//end if
   }// end of for loop
  }

 }
}

Labels: Java Jar Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger