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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:36:13+00:00 2026-06-05T20:36:13+00:00

NOTE: This is a followup to my question here. I have a program that

  • 0

NOTE: This is a followup to my question here.


I have a program that takes the contents of a directory and bundles everything into a JAR file. The code I use to do this is here:

    try
    {
        FileOutputStream stream = new FileOutputStream(target);
        JarOutputStream jOS = new JarOutputStream(stream);

        LinkedList<File> fileList = new LinkedList<File>();
        buildList(directory, fileList);

        JarEntry jarAdd;

        String basePath = directory.getAbsolutePath();
        byte[] buffer = new byte[4096];
        for(File file : fileList)
        {
            String path = file.getPath().substring(basePath.length() + 1);
            path.replaceAll("\\\\", "/");
            jarAdd = new JarEntry(path);
            jarAdd.setTime(file.lastModified());
            jOS.putNextEntry(jarAdd);

            FileInputStream in = new FileInputStream(file);
            while(true)
            {
                int nRead = in.read(buffer, 0, buffer.length);
                if(nRead <= 0)
                    break;
                jOS.write(buffer, 0, nRead);
            }
            in.close();
        }
        jOS.close();
        stream.close();

So, all is well and good and the jar gets created, and when I explore its contents with 7-zip it has all the files I need. However, when I try to access the contents of the Jar via a URLClassLoader (the jar is not on the classpath and I don’t intend it to be), I get null pointer exceptions.

The odd thing is, when I use a Jar that I’ve exported from Eclipse, I can access the contents of it in the way I want. This leads me to believe that I’m somehow not creating the Jar correctly, and am leaving something out. Is there anything missing from the method up above?

  • 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-05T20:36:16+00:00Added an answer on June 5, 2026 at 8:36 pm

    I figured it out based on this question – the problem was me not properly handling backslashes.

    Fixed code is here:

            FileOutputStream stream = new FileOutputStream(target);
            JarOutputStream jOS = new JarOutputStream(stream);
    
            LinkedList<File> fileList = new LinkedList<File>();
            buildList(directory, fileList);
    
            JarEntry entry;
    
            String basePath = directory.getAbsolutePath();
            byte[] buffer = new byte[4096];
            for(File file : fileList)
            {
                String path = file.getPath().substring(basePath.length() + 1);
                path = path.replace("\\", "/");
                entry = new JarEntry(path);
                entry.setTime(file.lastModified());
                jOS.putNextEntry(entry);
                FileInputStream in = new FileInputStream(file);
                while(true)
                {
                    int nRead = in.read(buffer, 0, buffer.length);
                    if(nRead <= 0)
                        break;
                    jOS.write(buffer, 0, nRead);
                }
                in.close();
                jOS.closeEntry();
            }
            jOS.close();
            stream.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This Question arises from a Q&A here I have some doubts that i think
NOTE: This is an entire rewrite of the previous question. I have a model.
Note: This question has broadened in scope from previous revisions. I have tried to
I ask this question as a followup of this question . A solution that
Note that this is a follow-up question from my previous one: How to memorize
This is a followup to my question here . I would like to understand
This is a follow up of another question here on SO . I have
Note, this is a follow up to my question here . I'm trying to
[ Note: This is a followup to the question about running an Android service
Follow-up question to this question : (note that this is not a duplicate, I'm

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.