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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:30:35+00:00 2026-05-15T09:30:35+00:00

I want to add some compiled classes (.class files) to directories(packages) in current Jar

  • 0

I want to add some compiled classes (.class files) to directories(packages) in current Jar file at runtime
How can I do that?

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-05-15T09:30:36+00:00Added an answer on May 15, 2026 at 9:30 am

    This cannot be done – To update a Jar file you need to create a new one and overwrite the old one with the new one.

    Below is a sample on how you would do this:

    import java.io.*;
    import java.util.*;
    import java.util.zip.*;
    import java.util.jar.*;
    
    public class JarUpdate {
       /**
        * main()
        */
       public static void main(String[] args) throws IOException {
          // Get the jar name and entry name from the command-line.
    
          String jarName = args[0];
          String fileName = args[1];
    
          // Create file descriptors for the jar and a temp jar.
    
          File jarFile = new File(jarName);
          File tempJarFile = new File(jarName + ".tmp");
    
          // Open the jar file.
    
          JarFile jar = new JarFile(jarFile);
          System.out.println(jarName + " opened.");
    
          // Initialize a flag that will indicate that the jar was updated.
    
          boolean jarUpdated = false;
    
          try {
             // Create a temp jar file with no manifest. (The manifest will
             // be copied when the entries are copied.)
    
             Manifest jarManifest = jar.getManifest();
             JarOutputStream tempJar =
                new JarOutputStream(new FileOutputStream(tempJarFile));
    
             // Allocate a buffer for reading entry data.
    
             byte[] buffer = new byte[1024];
             int bytesRead;
    
             try {
                // Open the given file.
    
                FileInputStream file = new FileInputStream(fileName);
    
                try {
                   // Create a jar entry and add it to the temp jar.
    
                   JarEntry entry = new JarEntry(fileName);
                   tempJar.putNextEntry(entry);
    
                   // Read the file and write it to the jar.
    
                   while ((bytesRead = file.read(buffer)) != -1) {
                      tempJar.write(buffer, 0, bytesRead);
                   }
    
                   System.out.println(entry.getName() + " added.");
                }
                finally {
                   file.close();
                }
    
                // Loop through the jar entries and add them to the temp jar,
                // skipping the entry that was added to the temp jar already.
    
                for (Enumeration entries = jar.entries(); entries.hasMoreElements(); ) {
                   // Get the next entry.
    
                   JarEntry entry = (JarEntry) entries.nextElement();
    
                   // If the entry has not been added already, add it.
    
                   if (! entry.getName().equals(fileName)) {
                      // Get an input stream for the entry.
    
                      InputStream entryStream = jar.getInputStream(entry);
    
                      // Read the entry and write it to the temp jar.
    
                      tempJar.putNextEntry(entry);
    
                      while ((bytesRead = entryStream.read(buffer)) != -1) {
                         tempJar.write(buffer, 0, bytesRead);
                      }
                   }
                }
    
                jarUpdated = true;
             }
             catch (Exception ex) {
                System.out.println(ex);
    
                // Add a stub entry here, so that the jar will close without an
                // exception.
    
                tempJar.putNextEntry(new JarEntry("stub"));
             }
             finally {
                tempJar.close();
             }
          }
          finally {
             jar.close();
             System.out.println(jarName + " closed.");
    
             // If the jar was not updated, delete the temp jar file.
    
             if (! jarUpdated) {
                tempJarFile.delete();
             }
          }
    
          // If the jar was updated, delete the original jar file and rename the
          // temp jar file to the original name.
    
          if (jarUpdated) {
             jarFile.delete();
             tempJarFile.renameTo(jarFile);
             System.out.println(jarName + " updated.");
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view that I want to add some custom drawing to. I
I want to add some logging capability to a cf.net application running on WM6
I want to add some Ajax -niceness to my Django-coded website. In my Django
Suppose I have some application A with a database. Now I want to add
I have dbml with single table users i want add partial class for User
I want to add a property to my User model that returns the number
I made a remote app for iPhone and now want to add some gestures
I have date in this format 1999-05-31T13:20:00.000-05:00 I want to add some hours or
I have date in this format 1999-05-31T13:20:00.000-05:00 I want to add some hours or
I want to add a column to an existing legacy database and write 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.