简体   繁体   English

如何为jar文件生成校验和

[英]How to generate a checksum for jar file

We want to implement a feature like this: when user firstly run the app, app will download a jar file to local PC from a server and load the jar file but when user run the app again, app can distinguish if the content of the jar file in local folder is same as the one on server, if so not download it again just load it from local folder; 我们要实现这样的功能:用户首次运行应用程序时,应用程序将从服务器将jar文件下载到本地PC并加载该jar文件,但是当用户再次运行该应用程序时,应用程序可以区分该jar文件的内容本地文件夹中的文件与服务器上的文件相同,如果不重新下载,则从本地文件夹中加载; if not app will download it and update local one (the jar file name on server and on local must be same). 如果不是,则应用将下载并更新本地文件(服务器和本地上的jar文件名必须相同)。 Currently one solution I can think out is to use checksum that is app generates the checksum of local jar file then get the checksum of server jar file from the server, check if they are same, if so not download again, if not download and update. 目前我能想到的一种解决方案是使用校验和,即应用程序生成本地jar文件的校验和,然后从服务器获取服务器jar文件的校验和,检查它们是否相同,如果不相同,则不再下载,如果不下载并更新。 Is there a simple way to generate checksum of a jar file? 有没有一种简单的方法来生成jar文件的校验和? Or is there any other better solution for this feature? 还是对此功能有其他更好的解决方案? Thanks. 谢谢。

Calculating the jar checksum is the same as calculating the checksum of any file. 计算jar校验和与计算任何文件的校验和相同。 But it looks like you need Java Web Start , which will take care of everything. 但是看起来您需要Java Web Start ,它将处理所有事情。

Seems I can use "java.util.zip.CRC32" to get checksum value of the jar file. 似乎我可以使用“ java.util.zip.CRC32”来获取jar文件的校验和值。 But I'm not sure whether there is the possiblilty that the checksum values of two jar files are same but in fact the two jar files are different (a bit different in content)? 但是我不确定两个jar文件的校验和值是否相同,但是实际上两个jar文件不同(内容有些不同)吗?

您可以使用Google Guava库中的Files.getChecksum(file)

If you are building the jar file with maven there is a maven plug-in for generating check-sums of a compiled jar. 如果使用maven构建jar文件,则有一个maven插件可用于生成已编译jar的校验和。 Maven Plugin Link . Maven插件链接

As for the downloaded jar you can locally SAVE ,rather than generate, the jars check sum. 至于下载的jar,您可以在本地保存,而不是生成jar校验和。 Fetch the check-sum from the server and send a request to the it every time you need to check if the jars check sum is changed and if so download the new one. 每次需要检查jar的校验和是否已更改时,都从服务器获取校验和并向其发送请求,如果是,则下载新的。

Java Web Start is certainly the right way to do it as Bozho says. 正如Bozho所说,Java Web Start当然是正确的方法。 For a hand-made alternative see What's the best way to add a self-update feature to a Java Swing application? 有关手工制作的替代方法,请参见向Java Swing应用程序添加自我更新功能的最佳方法是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM