Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java Get classes In Package 
 

In this section, you will learn how to retrieve all the classes from the package by providing the path of the jar file and the package name.

 

Java Get classes In Package

                         

In this section, you will learn how to retrieve all the classes from the package by providing the path of the jar file and the package name.  

 

 The class JarInputStream read the contents of the JAR file. The method getNextJarEntry () reads the JAR file and the positions of   the stream at the beginning of the entry data.

The class JarEntry represent a JAR file. The method getName() returns the name of Jar.

 

Following code adds all the classes of the package getting from the jar in the ArrayList.

arrayList.add (jarEntry.getName().replaceAll("/", "\\."))


Here is the code of GetClassesInPackage.java

import java.io.*;
import java.util.*;
import java.util.jar.*;

public class GetClassesInPackage {
private static boolean getJar = true;
public static List getClasseNamesInPackage
     (String jarName, String packageName){
   ArrayList arrayList = new ArrayList ();
   packageName = packageName.replaceAll("\\." "/");
   if (getJar) 
   System.out.println
        ("Jar " + jarName + " for " + packageName);
   try{
     JarInputStream jarFile = new JarInputStream(new FileInputStream (jarName));
     JarEntry jarEntry;
     while(true) {
       jarEntry=jarFile.getNextJarEntry ();
       if(jarEntry == null){
         break;
       }
       if((jarEntry.getName ().startsWith (packageName)) &&
            (jarEntry.getName ().endsWith (".class")) ) {
         arrayList.add (jarEntry.getName().replaceAll("/""\\."));
       }
     }
   }
   catch( Exception e){
     e.printStackTrace ();
   }
   return arrayList;
}
   public static void main (String[] args){
   List list =  GetClassesInPackage.getClasseNamesInPackage
        ("C://apache-tomcat-6.0.16//lib//jfreechart.jar""org.jfree.chart.plot");
   System.out.println("Found: "+list);
  
  }
}


Output will be displayed as:

 

Download Source Code

 

                         

» View all related tutorials
Related Tags: c class ant help property get tutorial name ria this for to e des pe in as sta m nt

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.