Java - 如何减小第三方 jar 的大小以减小应用程序的大小

2022-01-15 00:00:00 dependencies java jar

我正在开发一个 java web 应用程序,其中包括 一个小程序.那个小程序是依赖两个jar文件:

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

  • JFreeChart(用于在客户端绘制图表) - 1.7 mb(大小jar 文件)
  • MySqlJdbcConnector(用于存储在客户端捕获的数据,以远程数据库)- .7 mb(大小为jar 文件)
  • JFreeChart (for plotting graphs at the client side) - 1.7 mb(size of jar file)
  • MySqlJdbcConnector (for storing data, captured at the client side, to a remote database) - .7 mb (size of jar file)

现在,问题是上面的大小两个jar文件.我的总大小小程序 jar (myApplet.jar) 是 2.5mb 其中 2.4 mb 是因为上面的两个jar文件.

Now, the problem is the size of above two jar files. The total size of my applet jar (myApplet.jar) is 2.5 mb out of which 2.4 mb is because of the above two jar files.

我没有使用所有的类那些jar文件.具体来说,对于jfreechart,我使用的类非常少图书馆.

I am not using all the classes in those jar files. Specifically, for jfreechart, I am using a very small number of classes from that library.

===============================问题=======================================

==============================Questions======================================

Q1. 为了创建 myApplet.jar 文件,我已经解压缩了两个 jar 文件(jfreechart 和 mySQLJdbcConnector),然后打包了解压缩的版本jar 文件与我的小程序代码的源代码来创建一个单独的 jar 文件(即 myApplet.jar). 这是用您的小程序代码打包第三方 jar 文件的正确方法吗?有什么办法可以优化吗?

Q1. For creating myApplet.jar file, what I have done is I have unzipped both of the jar files (jfreechart and mySQLJdbcConnector) and then packed the unzipped version of the jar files with the source code of my applet code to create one single jar file (i.e myApplet.jar). Is it the correct way of packing third party jar files with your applet code? Is there any way by which I can optimize this?

Q2. 我试图找到我在我的应用程序中使用的 jfreechart 库的类的依赖项,以便仅将这些依赖项打包到 myApplet.jar.为此,我使用 DependencyAnalyzer 来查找所有类的依赖关系.但后来我发现手动这样做很困难,因为每个类(我在我的应用程序中使用的 jfreechart 类)都有很多依赖项,而且我使用了大约 15 个 jfreechart 类,所以对每个类都这样做会非常困难.那么对此有什么建议吗?

Q2. I tried to find the dependencies of the classes of jfreechart library which I am using in my application so as to pack only those dependencies in myApplet.jar. For that purpose, I used DependencyAnalyzer to find the dependencies of all the classes. But later I found it difficult to do so manually because every class (class of jfreechart that I am using in my application) has lot of dependencies and I am using some 15 classes of jfreechart so doing this for every class will be very difficult. So any suggestion on this?

Q3.这种情况是开发人员经常遇到的还是我错过了一些东西,因此我必须这样做?

Q3. Is this situation very common that developers encounter or I am missing something because of which I have to do this?

推荐答案

A1:

您可以创建一个 ant 脚本或使用 Eclipse 或任何其他 IDE 来自动打包您的小程序.不过你的方法也是对的

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

我不会手动做这些事情.寻找传递依赖是非常复杂的.也许 darioo 的回答是更好的方法.

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

这确实很常见.一些提示:

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.

另一方面,也许您不应该从您的小程序直接连接到数据库.您可以创建一个 RMI 接口(或类似的接口)来将您的 SQL 和结果数据传输到实际执行您的 SQL 的应用程序服务器.如果您不在安全的 Intranet 中运行,这对您的小程序来说是一个重要的安全方面.

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.

相关文章