<p>It may not be best practice but are there ways of removing unsused classes from a third party's jar files.  Something that looks at the way in which my classes are using the library and does some kind of coverage analysis, then spits out another jar with all of the untouched classes removed.</p>

<p>Obviously there are issues with this.  Specifically, the usage scenario I put it though may not use all classes all the time.</p>

<p>But neglecting these problems, can it be done in principle?</p>

[{'answer_id': 38309, 'author': 'Eugene Yokota', 'author_id': 3827, 'author_profile': 'https://Stackoverflow.com/users/3827', 'pm_score': 0, 'selected': False, 'text': "<p>jar is just a zip file, so I guess you can. If you could get to the source, it's cleaner. Maybe try disassembling the class?</p>\n"}, {'answer_id': 38315, 'author': 'Hoffmann', 'author_id': 3485, 'author_profile': 'https://Stackoverflow.com/users/3485', 'pm_score': 0, 'selected': False, 'text': '<p>Adding to this question, can that improve performance? Since the classes not used would not be JIT compiled improving startup time or does the java automatically detect that while compiling to bytecode and do not even deal with the code that is not used?</p>\n'}, {'answer_id': 38318, 'author': 'Paul Tomblin', 'author_id': 3333, 'author_profile': 'https://Stackoverflow.com/users/3333', 'pm_score': 1, 'selected': False, 'text': '<p>At a previous job, I used a Java obfuscator that as well as obfuscating the code, also removed classes and methods that weren\'t being used.  If you were doing "Class.byName" or any other type of reflection stuff, you needed to tell the obfuscator because it couldn\'t tell by inspecting the code what classes or methods called by reflection.</p>\n\n<p>The problem, of course, is that you don\'t know if other parts of the third party library are doing any reflection, and so removing an "unused" class might cause things to break in an obscure case that you haven\'t tested.</p>\n'}, {'answer_id': 38373, 'author': 'dbkk', 'author_id': 838, 'author_profile': 'https://Stackoverflow.com/users/838', 'pm_score': 0, 'selected': False, 'text': "<p>This would be an interesting project (has anyone done it already?) </p>\n\n<p>I presume you'd give the tool your jar(s) as a starting point, and the library jar to clean up. It could use reflection to determine which classes your jar(s) reference directly, and which are used indirectly down the call tree (this is not trivial at all, but doable). If it encounters any reflection code in any of the two places, it should give a very loud warning.</p>\n"}, {'answer_id': 38591, 'author': 'SaM', 'author_id': 883, 'author_profile': 'https://Stackoverflow.com/users/883', 'pm_score': 4, 'selected': True, 'text': '<p>There is a way.</p>\n\n<p>The JarJar project does this AFAIR. The first goal of the JarJar project is to allow one to embed third party libraries in your own jar, changing the package structure if necessary. Doing so it can strip out the classes that are not needed.</p>\n\n<p>Check it out at <a href="http://code.google.com/p/jarjar/" rel="noreferrer">http://code.google.com/p/jarjar/</a>.</p>\n\n<p>Here is a link about shrinking jars: <a href="http://sixlegs.com/blog/java/jarjar-keep.html" rel="noreferrer">http://sixlegs.com/blog/java/jarjar-keep.html</a></p>\n'}, {'answer_id': 40057, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 2, 'selected': False, 'text': "<p>There is a tool in Ant called a classfileset. You specify the list of root classes that you know you need, and then the classfileset recursively analyzes their code to find all dependencies.</p>\n\n<p>Alternatively, you could develop a good test suite that exercises all of the functions that you need, then run your tests under a test coverage tool. The tool will tell you which classes (and statement in them) were actually utilized. This could give you an even smaller set of code than what you'd find with static analysis.</p>\n"}, {'answer_id': 1788498, 'author': 'Lawrence Dol', 'author_id': 8946, 'author_profile': 'https://Stackoverflow.com/users/8946', 'pm_score': 2, 'selected': False, 'text': '<p>I use <a href="http://proguard.sourceforge.net" rel="nofollow noreferrer">ProGuard</a> for this. As well as being an excellent obfuscator, it has a code shrinking phase which can combine multiple JARs and then strip out any unused classes or class members.  It does an <strong>excellent</strong> job at shrinking.</p>\n'}]
['https://Stackoverflow.com/questions/38305', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2443/']