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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:45:21+00:00 2026-06-07T08:45:21+00:00

the URLClassLoader class fails to load classes from the jar created programatically using the

  • 0

the URLClassLoader class fails to load classes from the jar created programatically using the code listed below, whereas when i create a jar with the same classes using jar cf %jarname% %sources% it works fine. Is there a difference between the jars created using jar cf and JarOutputStream.

public static ByteArrayInputStream createJar(File file) throws IOException {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    JarOutputStream target = new JarOutputStream(bytes, manifest);
    for (File child : file.listFiles()) {
        addJarEntries(child, target, "");
    }
    target.flush();
    target.close();
    return new ByteArrayInputStream(bytes.toByteArray());
}

private static void addJarEntries(File source, JarOutputStream target, String path) throws IOException {
    BufferedInputStream in = null;
    try
    {
        if (source.isDirectory())
        {
            String name = path +source.getName() + File.separator;
            for (File nestedFile: source.listFiles())
                addJarEntries(nestedFile, target, name);
            return;
        }

        in = new BufferedInputStream(new FileInputStream(source));

        JarEntry entry = new JarEntry(path + source.getName());
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        while (true)
        {
            int count = in.read(buffer);
            if (count == -1)
                break;
            target.write(buffer, 0, count);
        }
        target.closeEntry();
    }
    finally
    {
        if (in != null)
            in.close();
    }
}

Best Regards,
Keshav

  • 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-07T08:45:22+00:00Added an answer on June 7, 2026 at 8:45 am

    The jar command uses JarOutputStream to create a JAR file (source code) so it cannot be the fault of that class per se. However, it is possible that you missed some important step in the JAR creation process. For instance, you may have included a malformed manifest.

    You should be able to compare your code with the source code of the jar command to see if you have missed something important.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am loading a java Class from a jar file using an URLClassLoader. String
I'm trying to load a class from a JAR file using reflection. This JAR
I'm trying to use Class.forName to dynamically load a class from a .jar file
In which case one would use a URLClassLoader to load a class from a
I am using the following code to dynamically load a class in java: URL
I load dynamically an external class from my eclipse rcp application with urlClassLoader. The
I want to load and execute external jar file using URLClassLoader. What is the
I am trying to load a class dynamically from a jar file. It worked
I have a custom class loader which extends from a URLClassLoader. I added a
I have a simple class that imports another class from another jar. Everything compiles

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.