0

I have a .war file in Tomcat server. The .war file contains a file that I would like to override. The webapp will load up the file from the classpath.

How can I modify the classpath so that the server will load an another file from the local disk instead? Basically I should add to start the classpath the directory where my replacement file resides. How can I do that?

Juha Syrjälä
  • 1,081
  • 10
  • 19

1 Answers1

1

Unfortunately, Java works on a first-come, first-serve basis. If you have two identical files in two different jars, for example, whichever one appears first in Tomcat's classpath hierarchy will be used. The Tomcat 5.5 documentation talks about the order specifically.

So if you'd like to have one library take precedence over the other, you'll have to make sure that the one receiving precedence appears (alphabetically) before the one you want to be overwritten in WEB-INF/lib.

(BTW, this is what is infamously known as jar hell)

Andrew M.
  • 11,182
  • 2
  • 35
  • 29
  • I do know how the java classpath operates. I'd like to know how I can manipulate classpath in Tomcat so that I could override one of the files inside war. – Juha Syrjälä Sep 02 '10 at 05:41
  • I did, check the second paragraph. Unzip the war, go to `WEB-INF/lib`, and plop the library you want to use as the override into that directory, with a name that precedes the one you want overriden. Usually, numbers work--i.e., `10-somelibrary.jar` would come before `somelibrary.jar`. You can't directly manipulate the classpath in Tomcat5. – Andrew M. Sep 02 '10 at 12:18
  • Ok, I was hoping that there would some less intrusive method. I would prefer if I didn't have to change contents of the war file. – Juha Syrjälä Sep 02 '10 at 18:57
  • Unfortunately, .war files are supposed to be entirely self contained web applications, which means everything you need to run it should be there. A better question might be, why are you trying to do this? But that would be a task for another question. :) – Andrew M. Sep 03 '10 at 01:57
  • Typically one would like to use same war file in production and testing environment. But there need to be different configuration files depending on the environment. – Juha Syrjälä Sep 03 '10 at 12:04