简体   繁体   English

从特定的jar文件引用一个类

[英]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 . 情况是...我的项目在类路径中有一些A.jar文件,其中包含许多重要的实用程序类A.jarA.jar是相当旧的文件,由于我们不确定其重要性和重要性,因此无法替换它持有...。现在,我需要ServletResponse I / F中的方法request.setCharacterencoding()response.setCharacterEncoding ,但是此A.jar包含ServletResponse的旧版本,由于上述原因,我无法使用上述两种方法。现在,我在类路径中引入了新的servlet-api.jar ,但是我的项目仍然从A.jar而不是从新的servlet-api.jar引用servletRespons e类。

Can you guys, please suggest me a way to get the reference of new servletResponse from servlet-api.jar rather than A.jar 大家好,请向我建议一种从servlet-api.jar而不是A.jar获取新servletResponse引用的方法

(Ps : I can not remove/modify the A.jar) (注:我无法删除/修改A.jar)

Thanks... 谢谢...

When you run the jar file, make the classpath reflect the order of the jars you want loaded. 运行jar文件时,使类路径反映要加载的jar的顺序。 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. 让我知道您是否需要确切的语法,但总的来说,如果您具有CLASSPATH = patch16.jar:patch15.jar:patch14.jar:...等,它将加载该类的第一个匹配副本,从patch16开始.jar然后patch15.jar等。

More info on exact syntax: http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html 有关确切语法的更多信息: http : //javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html

Have servlet-api.jar come before A.jar in your classpath. 让servlet-api.jar在类路径中的A.jar之前。

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? 如果期望Servlet API的旧实现但新的Servlet API已加载,则A.jar中的功能是什么? 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 您最好解压缩A.jar并将其重新打包为A-modified.jar

Actually JVM looks for classes sequentially trying load them from resources from the list. 实际上,JVM会顺序搜索类,尝试从列表中的资源中加载它们。 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. 例如,如果您的类路径看起来像new.jar;old.jar并且两个jar具有相同类的不同版本,则将使用new.jar的版本。

Please pay attention that if you are on Unix use : instead of ; 请注意,如果您使用的是Unix,请使用:而不是; .

But may I express my doubt. 但我可以表示怀疑。 Something sounds wrong in your project. 在您的项目中听起来有些错误。 Why do you have ServletRequest in your custom jar? 为什么在自定义jar中有ServletRequest I should use standard j2ee.jar or something like that to get this kind of standard classes from. 我应该使用标准的j2ee.jar或类似的东西来获取这种标准的类。 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. 据我所知,在过去的12年中,只有1种方法从servlet API中删除:能够执行servlet之间的通信。 And this happened about 10 years ago. 这发生在大约10年前。 So if you are using the latest version standard servlet API everything including old code should work. 因此,如果您使用的是最新版本的标准Servlet API,则包括旧代码在内的所有内容均应正常工作。

Simple solution, if these two ServletRqquest are in different package, just don't import the legacy one but access it with full package name. 一个简单的解决方案,如果这两个ServletRqquest位于不同的包中,则只需不导入旧版,而是使用完整的包名访问它。 Or you have to write your own class load to load that jar. 或者,您必须编写自己的类加载程序来加载该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 但是,您可以编写一个自定义的ClassLoader ,它可以对所需的类采取特殊的操作,特别是在这种情况下,从servlet-api.jar加载ServletResponse ,否则 servlet-api.jar的类路径中A.jar

ps Find out whoever created a class with the same package and name as ServletRequest and kill them. ps找出创建与ServletRequest相同的包和名称的类的人,然后杀死它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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