I need to determine the version of the binary file (jar, dll, exe, etc.) using Java. It is necessary that the solution be cross-platform. All the solutions that I found were platform dependent. Maybe someone has already solved a similar problem? Thank you in advance.

  • Do you want the program running on linux to determine the Windows version of the dll? And in linuh such a function is not provided to determine the version of windows files, because they are not interested in him. And vice versa. So? - Sergey
  • Not really. I want one jar to be able to run on both Windows and Linux, and in each case can determine the versions of the files of this system. - Alex

3 answers 3

I suggest to use JNI. Together with your java program, develop platform JNI dlls that implement your java API for determining file versions. Probably this is all there is, since you yourself state that you have found something somewhere. Supply your java-program with these dll-kami.

public class JavaVersion { public native Version nativeVersion(String filename); public static void main(String[] args) { if ("винмастдай".equals(System.getProperties().getProperty("os.name")) { System.load("version.dll"); } else if ("линух".equals(System.getProperties().getProperty("os.name")) { System.load("version.so"); } else { throw new Exception("Утюг не той системы"); } Version version = nativeVersion(args[0]); System.out.println(args[0] + " версия " + version); } } 
  • There is also a JNA - Java Native Access project. It seems to be simpler than JNI. For example, to call the functions of the windows kernel did not write a single line in C, all in java - Sergey

The version of the .jar file can be determined using the file /META-INF/manifest.mf included in the archive

Applications of the .exe format have the header described in this article: http://wiki.osdev.org/PE#PE_header

Here, the application version is defined by the mMajorImageVersion and mMinorImageVersion fields , which are in the secondary header (immediately after the primary header).

Similarly with .dll

  • If we talk about the structures from MS, it is better to give a link to their documentation: msdn.microsoft.com/ru-ru/library/windows/desktop/ ... The file version in Windows properties to MinorImageVersion has nothing to do. - Vladimir Martyanov
  • But even MSDN says that both fields are responsible for "Image Version", i.e., for the version of the object (application, library) to which this secondary title belongs - Awe2K
  • He can say anything, but they have nothing to do with the version in the file properties. In addition, the format of the version in the properties is something like this: Version: 1, 8, 71, 0 File version: 1, 8, 71, 0 In the two fields it does not climb, and the fields with the above information still zero. - Vladimir Martyanov

The cross platform way is to write the parsing of the corresponding file structures yourself. For MZPE (EXE, DLL, etc.), the information you are interested in is stored in the resources section.