saravanan subramanian
saravanan subramanian

Reputation: 11

Version of jar file by reading its manifest using powershell

I am running a powershell script to idenfity the versions. For DLL's and EXE's using the following function to get the version . I have a few other files with the extension .Jar. Is there a way I can use powershell to open the jar and get the version of it from their manifest.

Please let me know.

@{n='Version';e={$_.versioninfo.Fileversion}}

Upvotes: 1

Views: 2649

Answers (2)

jla
jla

Reputation: 7424

This works as a PowerShell script on Server 2016/PS 5.1 and Win 10/PS 7.1 without installing anything extra. It reads the MANIFEST.MF file in the zip archive and writes to standard output. This is option 4 found here: https://stackoverflow.com/a/37561878/101151

Be sure to pass the absolute path to [io.compression.zipfile]::OpenRead. It seems to bind to the first directory it was run in and re-use that for relative paths.

# Read the MANIFEST.MF from a Java .JAR (really a .zip) file and output to standard output

param(
    [Parameter(Mandatory=$true)][string]$jarname
)

# The following code is based on an answer at https://stackoverflow.com/a/37561878/101151
Add-Type -assembly "system.io.compression.filesystem"
$zip = [io.compression.zipfile]::OpenRead((Get-ChildItem $jarname).FullName)
$file = $zip.Entries | where-object { $_.Name -eq "MANIFEST.MF"}
$stream = $file.Open()

$reader = New-Object IO.StreamReader($stream)
$text = $reader.ReadToEnd()
$text

$reader.Close()
$stream.Close()
$zip.Dispose()

Upvotes: 0

Hassan Voyeau
Hassan Voyeau

Reputation: 3624

Looks like you have to extract from the jar file first. I downloaded java and tested myself using a jar file I also downloaded

& "C:\Program Files\Java\jdk1.8.0_191\bin\jar.exe" xvf junit-4.10.jar META-INF/MANIFEST.MF
get-content .\META-INF\MANIFEST.MF

RESULTS

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_26-b03-384-10M3425 (Apple Inc.)

That being said, please read here, Do we want single, complete answers? where Implementation-Version is mentioned, so make sure you know where the version is to be and if you can depend on this.

Upvotes: 2

Related Questions