Does anybody know how got get the version info for a executable/file via Java. The scenario is that I have a file on my local system and if the version on the server is newer then the one on my system I need to download the file from the server.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
After spending hours online and coding I found a solution using JNA to get the Version Information for a file.
If you mean the information you get in Property->Details on Windows, bear in mind that it's platform dependent! That being said SIGAR has Java bindings and a FileVersionInfo class that seems close to what you need.
There is a portable solution explained here (and this question is more or less a duplicate).
Basically, windows executable use the PE format (and linux ones ELF) and you can use the pecoff4j library to read this information in a portable way.
I modified Antonis Zafiropoulos's answer and made a handy class you can just drop into your project. Note that the fileInfoResult and verQueryVal lines must exist even though they don't appear to do anything.
For reference, a modified version of the code by GEverding, with JNA 4, using com.sun.jna.platform.win32
Due to portability issues I believe this kind of information is not actually available in java unless you access it using a less portable approach.
For example you could write a wrapper using JNI and C++ and use the GetFileVersionInfo API (see also this JavaWorld tip) of Windows to get that kind of information from the exe. Another approach would be to use a totally external application that outputs the version of the file and use the Runtime class to create a process and interact with that application.
Other approaches would require having access to the server and providing version checking from server side: