Java - How to reduce the size of third-party jars to reduce the size of your application

前端 未结 3 973
北恋
北恋 2020-12-19 00:35

I am developing a java web application and that includes an applet. That applet is dependent on two jar files:

<
相关标签:
3条回答
  • 2020-12-19 01:21

    Yes you can save space by creating a JAR containing only the classes that your applet requires. (I've seen this called an uber-JAR.)

    There are various tools for doing this; e.g. ProGuard, Zelix ClassMaster, a Maven plugin whose name I forget and so on.

    There are however a couple of issues, at least in the general case:

    • If your code uses dynamic loading (e.g. by calling Class.forName(className)), these tools generally cannot detect the dependency. So to avoid dynamically loaded classes being left out of the final JAR, you need to tell the tool the names of all of all classes that your application might explicitly load that way.

    • You need to take a look at the license of the third party library. IIRC, some licenses require you to include the library in your distributed artifacts in a way that allows people to substitute a different version of the library. One could argue that an uber-JAR makes this hard to do, and therefore could be problematic.

    JFreeChart is LGPL, and LGPL is a license that has the requirement above. However MySQL is GPL, which trumps LGPL, and this means that your applet must be GPL'ed ... if you distribute it.


    Finally, if you want to minimize the size of your applet JAR, you should NOT include your source code in the JAR. Source code should be in a separate JAR (or ZIP, TAR or whatever) file.

    0 讨论(0)
  • 2020-12-19 01:26

    A1:

    You can create an ant script or use Eclipse or any other IDE to automatically package your applet. But your way is correct, too

    A2:

    I wouldn't do these things manually. Finding transitive dependencies is very complex. Maybe darioo's answer is a better way to do this.

    A3:

    This is very common indeed. A couple of hints:

    You can always re-build those third party libraries without debug information. That should slightly decrease the size of those libraries.

    On the other hand, maybe you shouldn't have a direct connection from your applet to a database. You could create an RMI interface (or something similar) to transfer your SQL and result data to an application server, which actually executes your SQL. This is an important security aspect for your applet, if you don't run this in a safe intranet.

    0 讨论(0)
  • 2020-12-19 01:34

    I'd suggest trying out ProGuard. You can exclude parts of jar files you're not using.

    0 讨论(0)
提交回复
热议问题