Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Downloading and replacing a jar file using instrumentation

843829Dec 30 2007 — edited Jan 1 2008
Hi,

I am trying to make the following work.

When the VM starts, an instrumentation agent will download a jar file from a HTTP server and override an existing jar file, which is in the classpath. After that, the application should continue to work with the new jar file.

Using an instrumentation agent (that runs before the main method of the application), I managed to download the new jar file and replace the old one with it. The new file is checked and found ok. The problem is that I get ClassDefNotFoundException when the application starts, although the file path exists in the classpath. (I also checked that the URL of the jar exists in the URLClassLoader of the application).

Just for more information:
I checked that if I programmatically add a new URL to the URLClassLoader (by using reflection), the same path to the jar file, only written in a different way (it means it is used as a new key in the URLs map), the application manages to run. However, I don't want to use this technic in the code.

It seems that if I override a jar file that exists in the classpath with a new jar file with the same name after the VM has started the classloader can't use this file; it doesn't load the new file, unless I add a URL to the URL classloader with different String representation.

Am I doing something wrong?
Is there a way to use the new downloaded jar?

The following is the instrumentation's agent code. It implements the method premain that runs before the application's main method, downloads the new jar file and overrides the old jar file (MyJar.jar).
public class LoaderAgent 
{
	public static void premain(String agentArguments, Instrumentation instrumentation) throws Exception
	{	
		URL url = new URL("http://192.168.1.14:9999/MyJar.jar");
		System.out.println("Downloading file from: "+url);
		in = new BufferedInputStream(url.openStream());

		// deleting the old jar file
		File jar = new File("d:/myAppDir/lib/MyJar.jar");
		if (jar.exists())
		{
			jar.delete();
		}

		// overriding existing (old file)
		int size = 0;
		out = new BufferedOutputStream(new FileOutputStream(jar));
		while ((size = in.read(input)) >= 0)
		{
			out.write(input, 0, size);
		}
		out.flush();

		// closing the streams
		closeInputStream(in);
		closeOutputStream(out);
	}
}
Any help will be appreciated,
Shai
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 29 2008
Added on Dec 30 2007
1 comment
428 views