get Class Names In Jar Package - Java Reflection

Java examples for Reflection:package

Description

get Class Names In Jar Package

Demo Code

/* //ww w .  j  a  va  2 s.c o m
 * ClassListHelper.java
 * Christoph Egger
 * $Revision$
 * 
 * Copyright (C) 2010 FTW (Telecommunications Research Center Vienna)
 * 
 *
 * This file is part of BIQINI, a free Policy and Charging Control Function
 * for session-based services.
 *
 * BIQINI is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * For a license to use the BIQINI software under conditions
 * other than those described here, or to purchase support for this
 * software, please contact FTW by e-mail at the following addresses:
 * fpcc@ftw.at ??
 *
 * BIQINI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ?See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA ?02111-1307 ?USA
 */
//package com.java2s;

import java.io.FileInputStream;

import java.util.ArrayList;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

public class Main {


    public static ArrayList<String> getClassNamesInJarPackage(
            String _JarName, String _PackageName) {
        ArrayList<String> nameList = new ArrayList<String>();
        _PackageName = _PackageName.replaceAll("\\.", "/");
        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"))) {
                    nameList.add(jarEntry.getName().replaceAll("/", "\\."));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nameList;
    }
}

Related Tutorials