Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:45:08+00:00 2026-05-30T02:45:08+00:00

I need to count the number of compiled classes, interfaces and enums in a

  • 0

I need to count the number of compiled classes, interfaces and enums in a given jar file programmatically (so I need three separate numbers). Which API would help me? (I can’t use third party libraries.)

I’ve already tried quite tricky scheme which seems not always correct. Namely, I read each ZipEntry into a byte[] and then feed the result to my custom class loader which extends standard CalssLoader and just sends this byte[] to ClassLoader.defineClass (which is protect and couldn’t be called from application code directly). Full code is on the Pastebin.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:45:10+00:00Added an answer on May 30, 2026 at 2:45 am

    A jar file is a zip file with a specific pattern.
    You may use a ZipFile and a ZipEntry or their children classes JarFile and JarEntry.

    This code (a method of a custom classloader) will return a Map with arrays of each type of “class” you need.

    public Map<String, List<Class<?>>> loadAndScanJar(File jarFile)
            throws ClassNotFoundException, ZipException, IOException {
    
        // Load the jar file into the JVM
        // You can remove this if the jar file already loaded.
        super.addURL(jarFile.toURI().toURL());
    
        Map<String, List<Class<?>>> classes = new HashMap<String, List<Class<?>>>();
    
        List<Class<?>> interfaces = new ArrayList<Class<?>>();
        List<Class<?>> clazzes = new ArrayList<Class<?>>();
        List<Class<?>> enums = new ArrayList<Class<?>>();
        List<Class<?>> annotations = new ArrayList<Class<?>>();
    
        classes.put("interfaces", interfaces);
        classes.put("classes", clazzes);
        classes.put("annotations", annotations);
        classes.put("enums", enums);
    
        // Count the classes loaded
        int count = 0;
    
        // Your jar file
        JarFile jar = new JarFile(jarFile);
        // Getting the files into the jar
        Enumeration<? extends JarEntry> enumeration = jar.entries();
    
        // Iterates into the files in the jar file
        while (enumeration.hasMoreElements()) {
            ZipEntry zipEntry = enumeration.nextElement();
    
            // Is this a class?
            if (zipEntry.getName().endsWith(".class")) {
    
                // Relative path of file into the jar.
                String className = zipEntry.getName();
    
                // Complete class name
                className = className.replace(".class", "").replace("/", ".");
                // Load class definition from JVM
                Class<?> clazz = this.loadClass(className);
    
                try {
                    // Verify the type of the "class"
                    if (clazz.isInterface()) {
                        interfaces.add(clazz);
                    } else if (clazz.isAnnotation()) {
                        annotations.add(clazz);
                    } else if (clazz.isEnum()) {
                        enums.add(clazz);
                    } else {
                        clazzes.add(clazz);
                    }
    
                    count++;
                } catch (ClassCastException e) {
    
                }
            }
        }
    
        System.out.println("Total: " + count);
    
        return classes;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to count the number of classes in correct C# source file. I
I need to count the number of lines in a file, in a UNIX
I need to count the number of cells in a column which contain a
I need to count number of repeating and position where they repeat of all
I've got some tests that need to count the number of warnings raised by
I need a function count_permutations() that returns the number of permutations of a given
I need to get a count of the number of files in a directory.
I have a a large text file (over 70mb) and need to count the
Simply I need to write echo t${count} = $t${count} To a text file, including
I need to count number of email addresses in a db that have 3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.