简体   繁体   English

Java类:如何检查兼容性以避免在运行时出现类加载/兼容性问题

[英]Java classes : how to check for compatibility to avoid classloading / compatibility issues at runtime

I have an Ant-based build system and I thought I might write a python script to test all compiled / jarred classes in a directory hierarchy so as to avoid problems that might arise from different version being used in compile time versus runtime. 我有一个基于Ant的构建系统,我想我可以编写一个python脚本来测试目录层次结构中的所有已编译/受干扰的类,以避免在编译时与运行时使用不同版本而引起的问题。

Naively the script initially checked the sha256 hash of all classes found in various jar , war and ear files and complains if the same class is found with a different hash. 天真的脚本最初检查了在各种jarwarear文件中找到的所有类的sha256哈希,并抱怨是否找到了具有不同哈希值的同一个类。 However this produces too many false alarms. 但是,这会产生过多的错误警报。

For instance the class org.apache.commons.collections.FastHashMap is available in both commons-beanutils-1.8.0.jar and commons-collections-2.1.1.jar with different hashes. 例如,在具有不同哈希值的commons-beanutils-1.8.0.jarcommons-collections-2.1.1.jar中都可以使用org.apache.commons.collections.FastHashMap类。 Obviously this criterion was too strict. 显然,这个标准太严格了。 In the end however, the only way I could produce the same hash was by doing the following: 但是最后,我可以产生相同散列的唯一方法是执行以下操作:

javap commons-beanutils-1.8.0.jar.exploded/org/apache/commons/collections/FastHashMap.class | sort | grep -v Compiled\ from\ |  sha256sum -
javap commons-collections-2.1.1.jar.exploded/org/apache/commons/collections/FastHashMap.class | sort | grep -v Compiled\ from\ |  sha256sum -

And these are indeed the same, but this, (especially the grep -v and the sort ) is very unsatisfactory. 这些确实是相同的,但是,这(特别是grep -vsort )非常不令人满意。 Is there a better way to test that all classes in a directory hierarchy are compatible so as to avoid unexpected runtime problems? 有没有更好的方法来测试目录层次结构中的所有类是否兼容,从而避免意外的运行时问题?

I suggest you test the application before you deploy it into production. 我建议您在将应用程序部署到生产环境之前对其进行测试。 If there are any libraries which you don't think are needed you should remove them or you should test them. 如果您认为不需要任何库,则应将其删除或进行测试。

You shouldn't be running a library in production which you have never used before even in development. 您不应该在生产中运行从未在开发中使用过的库。

This will minimise a wide variety of problems which could happen in production. 这将最大程度地减少生产中可能发生的各种问题。

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

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