Reputation: 147
I have been stuck in quite awkward situation in my program.
The case is that... my project is having some A.jar file in classpath which packs many important utility classes..but this A.jar
is quite old file and we cant replace this as we are not sure how and what importance it holds....
Now, i need the method request.setCharacterencoding()
& response.setCharacterEncoding
from ServletResponse I/F but this A.jar
contains the old version of ServletResponse
and due to which I am not getting the above two methods.. Now i have introduced new servlet-api.jar
in classpath but still my project is taking the reference of servletRespons
e class from A.jar
and not from new servlet-api.jar
.
Can you guys, please suggest me a way to get the reference of new servletResponse
from servlet-api.jar
rather than A.jar
(P.s. : I can not remove/modify the A.jar)
Thanks...
Upvotes: 3
Views: 7267
Reputation: 425033
Without doing anything special, you'd be screwed: Classes are loaded in the order they are found in the classpath.
However, you can write a custom ClassLoader
that can take special action for the classes you need, specifically in this case loading ServletResponse
from servlet-api.jar
, otherwise having A.jar
in the classpath before servlet-api.jar
p.s. Find out whoever created a class with the same package and name as ServletRequest and kill them.
Upvotes: 0
Reputation: 115328
Actually JVM looks for classes sequentially trying load them from resources from the list. If for example, your classpath looks like new.jar;old.jar
and both jars have different versions of the same class the version from new.jar
will be used.
Please pay attention that if you are on Unix use :
instead of ;
.
But may I express my doubt. Something sounds wrong in your project. Why do you have ServletRequest
in your custom jar? I should use standard j2ee.jar
or something like that to get this kind of standard classes from. As far as I know there is only 1 method that was removed from servlet API during the last 12 years: ability to peform servlet-to-servlet communication. And this happened about 10 years ago. So if you are using the latest version standard servlet API everything including old code should work.
Upvotes: 1
Reputation: 141
Have servlet-api.jar come before A.jar in your classpath.
However, your problems are not all solved! What does something in A.jar do if it expects the old implementation of the Servlet API, but the new one has been loaded? Or what happens when somebody else doesn't have the classpath in the same order?
You may be better off unzipping A.jar and repackaging it as A-modified.jar
Upvotes: 1
Reputation: 2534
The only way I see is to get or create a ClassLoader
, then get your content of your class as a byte array, then load your class.
Upvotes: 0
Reputation: 1746
Simple solution, if these two ServletRqquest are in different package, just don't import the legacy one but access it with full package name. Or you have to write your own class load to load that jar.
Upvotes: 0
Reputation: 1243
When you run the jar file, make the classpath reflect the order of the jars you want loaded. Let me know if you need to exact syntax, but in general, if you have CLASSPATH=patch16.jar:patch15.jar:patch14.jar:... etc, it will load the first matching copy of the class, starting with patch16.jar then patch15.jar etc.
More info on exact syntax: http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html
Upvotes: 5