从第三方库和 JAR 中删除不必要的类

发布于 2024-12-04 06:03:12 字数 164 浏览 2 评论 0原文

我需要从第三方 JAR 中删除未使用的类。为什么我应该使用工具?

我已经尝试使用 ProGuard。但是,它仅从项目本身中删除未使用的类,但第三方库 jar 始终保持不变。

I need to remove unused classes from third party JARs. Why tools should I use?

I already tried to use ProGuard. However, it removes only unused classes from the project itself but the library jars - third party - always remain unchanged.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽你眉间 2024-12-11 06:03:12

您可以创建一个 uber jar,然后使用 ProGuard。将库类重新打包到 jar 中将是一个挑战,但从您的问题的精神来看,您会更喜欢 uber jar 本身。

正如其他发帖者评论的那样,您仍然需要小心通过如此多的滥用和误解的反射机制加载的类。

You can create an uber jar and then use ProGuard. Repackaging library classes into jars would be a challenge, but from the spirit of your question you will prefer the uber jar as such.

As other posters have commented, you still need to be careful about classes loaded through the so much abused and misunderstood reflection mechanism.

不打扰别人 2024-12-11 06:03:12

@Joonas Pulakka 是对的。但是,如果您仍然确实想这样做,并确保您的应用程序不会因 ClassNotFoundException 失败,请使用选项 -verbose:class 运行您的应用程序,执行所有存在的用例,获取包含所有加载的日志类。然后获取第三方库的所有类的列表,并将库中从未加载的所有类归档。然后创建仅包含“需要”类的替代 jar 文件并祈祷:)

祝你好运。

@Joonas Pulakka is right. But if you still really want to do this and be sure that your application will not fail for ClassNotFoundException run your application with option -verbose:class, perform all usecases that exist, take the log that contains all loaded classes. Then take list of all classes of your third party library and file all classes from your library that have been never loaded. Then create alternative jar file that contains only "needed" classes and pray :)

Good luck.

时光无声 2024-12-11 06:03:12

知道您正在使用哪些类和库是一件好事,即使删除未使用的东西存在风险(正如彼得所指出的),携带任何类型的超额行李都是有成本的,您不应该仅仅这样做不断积累。如果您使用反射,那么请了解您使用它的目的,并系统地删除您不需要的内容。您可以更好地理解更精简的代码库的好处。

It is a good thing to know what classes and libraries you are using, and even if there is risk (as pointed out by Peter) to removing unused stuff, there is cost in carrying any kind of excess baggage, and you shouldn't just keep accumulating. If you use reflection, then get a handle on what you are using it for, and systematically get rid of what you don't need. There are benefits to a leaner code-base that you understand better.

染火枫林 2024-12-11 06:03:12

Java 仅在使用时加载类。删除类只会给您带来问题,并且不会在运行时为您提供帮助。 36 MB 的代码并不算多,因为只加载了其中的一部分。你有多少内存?现在大多数 PC 至少有 2000 MB 如果您通过慢速链接下载 applet 或 Java WebStart 应用程序,我想您正在使用 pack200(使 jar 更小)并且已经包含了最少的库。

Java only loads class as they are used. Removing classes can only cause you problems and won't help you at runtime. 36 MB of code isn't that much given only a portion of it will be loaded. How much memory do you have? Most PC have at least 2000 MB these days If you are downloading your applet or Java WebStart application over a slow link I would imagine you are using pack200 (to make the jars smaller) and have included the minimum of libraries already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文