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 8751467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:03:15+00:00 2026-06-13T13:03:15+00:00

I am trying to add JAR file to class path and load all classes

  • 0

I am trying to add JAR file to class path and load all classes from JAR file at run time. here is the code I wrote for this task (This class extends URLClassLoader)

public void loadJar(final String fName) throws IOException, IllegalAccessException, ClassNotFoundException {
    final File file = new File(fName);
    if (file.exists() && getFileExtension(file.getName()).equalsIgnoreCase("jar")) {
        addURL(file.toURI().toURL());
        for(final URL url : getURLs()){
            System.out.println(url.toString());
        }
        final ZipFile jarFile = new ZipFile(file, ZipFile.OPEN_READ);
        final Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) jarFile.entries();
        while (entries.hasMoreElements()) {
            final String className = getClassCanonicalName(entries.nextElement());
            if (className != null) {
                loadClass(getClassCanonicalName(entries.nextElement()));
            }
        }
    }
}

private String getFileExtension(final String fileName) {
    return fileName.substring(fileName.lastIndexOf(".") + 1);
}

private String getClassCanonicalName(final ZipEntry entry) {
    final String entryName = entry.getName();
    if (getFileExtension(entryName).toLowerCase().endsWith("class")) {
        return entryName.replaceAll(File.separator, ".");
    } else {
        return null;
    }
}

But I keep getting ClassNotFoundException for class entities even through getURLs does indicate jar files has been added to this loader.

What is the cause of this problem? Thanks

  • 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-06-13T13:03:16+00:00Added an answer on June 13, 2026 at 1:03 pm
    return entryName.replaceAll(File.separator, "."); 
    

    On Windows this will fail. It should be / for the separator of a ZipEntry for a Zip made on any platform.

    So replace that with:

    return entryName.replaceAll("/", "."); 
    

    Also strip the class name. SSCCE E.G.:

    import java.io.*;
    import java.net.*;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class URLClassLoaderTest extends URLClassLoader {
    
        public URLClassLoaderTest(URL[] arg0) {
            super(arg0);
        }
    
        public void loadJar(URL urlOfJar) throws IOException, IllegalAccessException, ClassNotFoundException {
            if (getFileExtension(urlOfJar.getFile()).equalsIgnoreCase("jar")) {
                addURL(urlOfJar);
                for(final URL url : getURLs()){
                    System.out.println(url.toString());
                }
                final ZipInputStream zis = new ZipInputStream(urlOfJar.openStream());
                ZipEntry ze = zis.getNextEntry();
                while (ze!=null) {
                    final String className = getClassCanonicalName(ze);
                    if (className != null) {
                        loadClass(getClassCanonicalName(ze));
                    }
                    ze = zis.getNextEntry();
                }
            }
        }
    
        private String getFileExtension(final String fileName) {
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        }
    
        private String getClassCanonicalName(final ZipEntry entry) {
            final String entryName = entry.getName();
            if (getFileExtension(entryName).toLowerCase().endsWith("class")) {
                String s = entryName.substring(0,entryName.length()-6);
                s = s.replaceAll("/", ".");
                System.out.println(s);
                return s;
            } else {
                return null;
            }
        }
    
        public static void main(String[] args) throws Exception {
            URL[] url = {new URL("http://pscode.org/lib/mime.jar")};
            URLClassLoaderTest uclt = new URLClassLoaderTest(url);
            uclt.loadJar(url[0]);
        }
    }
    

    Output

    http://pscode.org/lib/mime.jar
    org.pscode.mime.MimeType$1
    org.pscode.mime.MimeType$1
    org.pscode.mime.MimeType$2
    org.pscode.mime.MimeType$2
    org.pscode.mime.MimeType
    org.pscode.mime.MimeType
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to add the following jar file: tika-app-1.1 Here is the Console
I'm trying to update (add/modify files) an existing JAR file, and this code (using
I'm trying to compile and run the task-android-sample code from Google's API website. I
I am trying to add jar file to classpath at runtime. I use this
I've been trying all day to turn my javafx application into a jar file.
I'm trying to run a java class that reads off some values from a
I'm trying add a directory of jar files (or barring that, each jar file
I am trying to add all the jar files in the directory below but
I'm trying to add external JAR file to eclipse android project. JAR package have
I'm trying to find a path to a jar file that's in the raw

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.