简体   繁体   中英

Refer a class from specific jar file

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

(Ps : I can not remove/modify the A.jar)

Thanks...

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

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

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.

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.

我看到的唯一方法是获取或创建ClassLoader ,然后将您的类的内容作为字节数组获取,然后加载您的类。

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

ps Find out whoever created a class with the same package and name as ServletRequest and kill them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM