Hi,

below is the code which I'm using to update a jar, unfortunately it is not updating the jar file which is in use by some program. I mean the jar itself has this code to update it's own file. The code is working if I want to update another jar file which is not in use.

An I doing it right?

public void updateJarFile(File srcJarFile, String targetPackage, File ...filesToAdd) throws IOException {
        File tmpJarFile = File.createTempFile("tempJar", ".tmp");
        JarFile jarFile = new JarFile(srcJarFile);
        boolean jarUpdated = false;
        //updateJarFile1( srcJarFile,  targetPackage,  filesToAdd);
        String name ="";
        try {
            JarOutputStream tempJarOutputStream = new JarOutputStream(new FileOutputStream(tmpJarFile));

            try {
                //Added the new files to the jar.
                for(int i=0; i < filesToAdd.length; i++) {
                    File file = filesToAdd[i];
                    JOptionPane.showMessageDialog(null,filesToAdd[i]);
                    name =  targetPackage+"/"+file.getName();
                    FileInputStream fis = new FileInputStream(file);
                    try {
                        byte[] buffer = new byte[1024];
                        int bytesRead = 0;
                        JarEntry entry = new JarEntry(name);
                        tempJarOutputStream.putNextEntry(entry);
                        while((bytesRead = fis.read(buffer)) != -1) {
                            tempJarOutputStream.write(buffer, 0, bytesRead);
                        }

                        System.out.println(entry.getName() + " added.");
                    }
                    finally {

                        fis.close();
                    }
                }


                 Enumeration jarEntries = jarFile.entries();
                    while(jarEntries.hasMoreElements()) {

                        JarEntry entry = (JarEntry) jarEntries.nextElement();
                    //    JOptionPane.showMessageDialog(null,entry.getName().toString()+ " fadsfanb.");
                        InputStream entryInputStream = jarFile.getInputStream(entry);
                        tempJarOutputStream.putNextEntry(entry);
                        byte[] buffer = new byte[1024];
                        int bytesRead = 0;
                        while ((bytesRead = entryInputStream.read(buffer)) != -1) {
                            tempJarOutputStream.write(buffer, 0, bytesRead);
                        }
                    }

                jarUpdated = true;
            }
            catch(Exception ex) {
                JOptionPane.showMessageDialog(null,ex.toString()+ " gvfdag.");
                tempJarOutputStream.putNextEntry(new JarEntry("stub"));
            }
            finally {
                tempJarOutputStream.close();
            }

        }
        finally {
            jarFile.close();
            //System.out.println(srcJarFile.getAbsolutePath() + " closed.");

            if (!jarUpdated) {
                tmpJarFile.delete();
            }
        }

        if (jarUpdated) {
            srcJarFile.delete();
            tmpJarFile.renameTo(srcJarFile);
            //System.out.println(srcJarFile.getAbsolutePath() + " updated.");
        }
    }

I would be surprised if its possible to update a jar that's in use. And if you could I'd be even more surprised if it didn't result in some kind of crash.

Maybe, if you can explain what your end objective is, we can find another way to do it?

I'm developing a plugin (jar file) for a software. Once it is done, many users will be using it. I will be having new files in the server every 2 weeks. Instead of delivering plugin across all users located at various places, I would like to update their installed plugin with those files on the server. The problem is, the plugin can be triggered with the software and once the software is open the plugin is in use. I cannot update it.

Is it possible to achieve it

Sorry, I don't know how to do that - hopefully someone else can help.
J

save the files on the server, but not in the jar. that way, you can update it. but updating a running jar .. can't see any logical way how that would work without crashing the application itself.

*If *the code in your jar isn't called very intensively, and doesn't hold state info from one call to the next, then maybe you could refactor it as a service that the clients call (eg via a Socket connection). That way they will always get the latest version of the code each time they call it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.