Skip to content

Instantly share code, notes, and snippets.

@tomazas
Last active May 4, 2023 11:39
Show Gist options
  • Save tomazas/0f8b6c99f50005c40e4e20c92c452359 to your computer and use it in GitHub Desktop.
Save tomazas/0f8b6c99f50005c40e4e20c92c452359 to your computer and use it in GitHub Desktop.
Detect duplicate Java classes after scanning all JAR/WAR files in provided directory
# Script detects duplicate Java classes after scanning all JAR/WAR files in provided directory.
# usage: python script <dir>
import zipfile
import sys
import os
if len(sys.argv) < 2:
print("usage: python %s <dir>"%(sys.argv[0]))
sys.exit(1)
dict = {}
problems = []
for root, dirs, files in os.walk(sys.argv[1]):
for file in files:
abspath = root + os.sep + file
if abspath.endswith(".jar") or abspath.endswith(".war"):
print("checking:",abspath)
with zipfile.ZipFile(abspath, 'r') as zip_ref:
for classfile in zip_ref.namelist():
if "module-info" in classfile: continue
if classfile.endswith(".class"):
if classfile in dict: # duplicate!
dict[classfile].append(abspath)
problems.append(classfile)
else:
dict[classfile] = [abspath]
print()
print(str(len(problems)),"duplicate classes found")
if len(problems) > 0:
for dup in problems:
print(dup," found in files:")
for e in dict[dup]:
print("\t" + e + ",")
print()
print("done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment