Selective jar packaging

陌路散爱 提交于 2019-12-10 17:56:30

问题


I have my small program.jar that uses a small part of huge library.jar.

Is there a tool to repackage several jars into one so it can be run stand-alone and is as small as possible?

Update: size matters.


回答1:


There is proguard, with ant and maven plugins. It removes unused code, optionally obfuscates and compresses to a single jar.

Proguard reduces the size on two fronts

  1. only the classes that are actually used are stored in the final jar.
  2. Long names, e.g. all the long package names, class names, are renamed to much shorter names, saving quite a bit of space.

Determining whether a class is used in java is a bit tricky. Static code analysis can get you so far, but you need to be a bit careful code accessed via reflection, since the tool may not be able to detect that. It handles Class.forName("x.y.z.Foo") but anything more complex and you'll need to add it to a config file of classes to include, It's possible to have a classloader generate a list of classes that are used at runtime, so this doesn't have to be arduous.

You might also want to investigate the pack200 compression scheme, as this can also bring a significant reduction in size of the jar.




回答2:


JarJar is an ant based solution:

http://code.google.com/p/jarjar/

Or if you use maven, there's the shade plugin:

http://maven.apache.org/plugins/maven-shade-plugin/




回答3:


You can try using the Ant jar task - more info can be read here.



来源:https://stackoverflow.com/questions/2888119/selective-jar-packaging

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!