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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:04:23+00:00 2026-06-09T11:04:23+00:00

I am building an application that must load the current class at runtime, and

  • 0

I am building an application that must load the current class at runtime, and add it to a different .jar that I am creating. I have a method that adds files to a jar.

private static void add(File source, JarOutputStream target,
        Manifest manifest) throws IOException {
    BufferedInputStream in = null;
    try {
        String name = source.getName();
        JarEntry entry = new JarEntry(name);
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        in = new BufferedInputStream(new FileInputStream(source));

        byte[] buffer = new byte[1024];
        while (true) {
            int count = in.read(buffer);
            if (count == -1)
                break;
            target.write(buffer, 0, count);
        }
        target.closeEntry();
    } finally {
        if (in != null)
            in.close();
    }
}

My problem is this: I can’t seem to find out how to add the current class into a file at runtime. I have tried this:

File classFile= new File(getClass().getResource("MyClass.class").getPath());

but I get a null pointer exception. Any help would be greatly appreciated.

  • 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-09T11:04:25+00:00Added an answer on June 9, 2026 at 11:04 am

    Don’t try to get the class file as a File – just fetch the stream directly:

    InputStream classFile = getClass().getResourceAsStream("MyClass.class");
    

    You’ll need to modify your add method to take a target name and an input stream, of course. (Potentially overload it, so you’ve still got the existing method available, which would just open the file, call the other method, then close the stream.)

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

Sidebar

Related Questions

I am building an application that must add an overlay view once a scrollview
I'm building an application that is used by several different customers. Each customer has
If I'm building an application that will have over 30 models, and I want
I'm building an application that already has it's Iphone version. I must build the
I'm building an application that must support MSSQL and MySQL databases. To avoid duplication
I am building an application which has requirements stating that all browser features must
We are building an application that will have clients installed in many sites. Each
We have an existing MVC application that we're building using TeamCity and things were
I have a WPF C# application that makes use of the InkAnalyzer class. On
Background: We're building an application that allows our customers to supply data in a

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.