William Norris
William Norris

Reputation: 78

How to get the current Java version available?

How do i get the current Java version available using c#?

I have been able to find a way to get the java version on my PC using C# but I need to be able to compare the installed version with the most current version available. At the moment I am unable to find an API or web service to get this information.

Hopefully someone here knows the answer to this.

Upvotes: 4

Views: 5248

Answers (3)

Chris Ballance
Chris Ballance

Reputation: 34337

Latest version available

You can pull the current version available for download from this page:

http://java.com/en/download/index.jsp

using this selector

.jvc0w2 > strong:nth-child(3)

A library like HTML Agility Pack for C# makes this straightforward.

Version currently installed

RegistryKey rk = Registry.LocalMachine;
RegistryKey subKey = rk.OpenSubKey("SOFTWARE\\JavaSoft\\Java Runtime Environment");

string currentVerion = subKey.GetValue("CurrentVersion").ToString();

Caveats

You will need to reconcile the formats of these two strings for comparison.

If the website I referenced to check the current version changes, you will may need to update your selector.

Upvotes: 7

Lee Meador
Lee Meador

Reputation: 12985

Find all the java.exe files and run them as "java -version". The output looks something like this:

java version "1.6.0_35"
Java(TM) SE Runtime Environment (build 1.6.0_35-b10)
Java HotSpot(TM) Client VM (build 20.10-b01, mixed mode, sharing)

So you can tell the version.

You should also look for the folder structure used by an installed java version.

Upvotes: 1

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

Try:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "javac";
psi.UseShellExecute = true;
psi.RedirectStandardError = true;

psi.Arguments = "-version";
Process p = Process.Start(psi);
string strOutput = p.StandardError.ReadToEnd();
p.WaitForExit();

Then you should parse the string of output. This output normally looks like:

javac 1.6.0_27

In case you are interested in the Runtime Environment (RE), replace javac with java

Upvotes: 3

Related Questions