Thursday, April 28, 2011

NSIS - How to Detect Whether Java is Installed in the System

While writing my installer I wanted to install JRE (embed JRE installer in my installer) only if Java is not installed already. So I had to detect whether Java is already installed.

My previous post on NSIS points to a quick start guide (in Harshani's blog) for NSIS, including how to install NSIS and how to write a simple installer. So at this point you should have installed NSIS and been somewhat familiar with NSIS scripting (sections, etc..). So here I am posting the piece of script I used to detect whether Java is installed in the system.

Section "find java" FINDJAVA

  StrCpy $1 "SOFTWARE\JavaSoft\Java Runtime Environment"
  StrCpy $2 0
  ReadRegStr $2 HKLM "$1" "CurrentVersion"
  StrCmp $2 "" DetectTry2
  ReadRegStr $5 HKLM "$1\$2" "JavaHome"
  StrCmp $5 "" DetectTry2
  goto done

  DetectTry2:
  ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Development Kit" "CurrentVersion"
  StrCmp $2 "" NoJava
  ReadRegStr $5 HKLM "SOFTWARE\JavaSoft\Java Development Kit\$2" "JavaHome"
  StrCmp $5 "" NoJava done

  done:
  ;All done. 

  NoJava:
  ;Write the script to install Java here

SectionEnd


In 3rd line of the script, it copies the path "Software\JavaSoft\Java Runtime Environment" to the variable 1. It is the Windows registry entry in which we should look for Java. In line 5 it reads the value of registry attribute "Current Version" which belongs to "SOFRWARE\JavaSoft\Java Runtime Environment", to the variable 2. ReadRegStr is the command used to read the registry. Here, "HKLM" means that it should look for this key under "HKEY_LOCAL_MACHINE" registry main key. 

In line 6 the command StrCmp compares the value of var 2 with "empty". If it is empty then that means there is no JRE installed in the system. So it passes the control to label "DetectTry2" which tries to detect if there is any Jdk installed. Line 7 is executed only if there is a value for JRE version. If so it tries to find the value for JavaHome attribute in key "Software\JavaSoft\Java Runtime Environment\1.x.x". In line 8 it checks whether JavaHome is null and if so, then again it jumps to label "DetectTry2". If it is not null that means there is a JRE installed and line 9 passes it to label "done".

Now let's see what "DetectTry2" label does. It is executed only if there is no JRE installed. In line 12 it reads the value of  "CurrentVersion" attribute of key "SOFTWARE\JavaSoft\Java Development Kit". In line 13 it checks whether the value it got is empty. If so it will hit the label "NoJava" means that neither JRE nor JDK is installed. If it is not empty then line 14 will check whether JavaHome is empty like before. Again, if JavaHome is empty it jumps to "NoJava". If not it jumps to "done".

If we ever hit "done" then that means either JRE or JDK is installed in the system. So there is nothing to do there. If we hit NoJava then we are going to install Java. I will post the script for that in a later post, NSIS - How to embed some other installer in your own installer.

This is valid for any other software like .NET framework, MS Office, etc as long as you know the correct registry entries to look in. 

15 comments:

Abu Lulon said...

Thanks, it helped me

kalani Ruwanpathirana said...

You are welcome..

A.R.MANOHAR said...

hey.. thanx, nice explanation...!

DHL said...

Thanks!

Champion Of Cyrodiil said...

Appreciate the Post. Much easier to understand then some of the others.

Anonymous said...

Thanks a lot for the script!

it helped me a lot

I do know it is a bit late since you posted this entry but i think its always good to leave comment

kalani Ruwanpathirana said...

Thank you everyone.

@Azaak : It's always good to have comments from you all :)

Unknown said...

Hello, thank you for sharing this code.

Notice that it will work only if you have a 32 bits java installed, it won't work for 64 bits java, you'll need to handle regview issues ...

Unknown said...
This comment has been removed by the author.
Anonymous said...

Tech Blog is a place for our technical staff to discuss everything from the technology that drives our product to our engineering culture.

Tech Blog

Unknown said...

good explanation Kalani :)

Unknown said...

good explanation Kalani :)

Pawel Co Faddey said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training in India . Nowadays Java has tons of job opportunities on various vertical industry.

Unknown said...

Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.


java training in bangalore

Unknown said...

In this fashion my partner Wesley Virgin's biography starts with this SHOCKING and controversial VIDEO.

Wesley was in the military-and soon after leaving-he revealed hidden, "mind control" secrets that the CIA and others used to obtain whatever they want.

THESE are the EXACT same SECRETS tons of celebrities (especially those who "became famous out of nothing") and the greatest business people used to become rich and successful.

You probably know how you only use 10% of your brain.

That's mostly because the majority of your brain's power is UNCONSCIOUS.

Perhaps this conversation has even occurred IN YOUR very own mind... as it did in my good friend Wesley Virgin's mind seven years back, while driving a non-registered, beat-up trash bucket of a car without a driver's license and with $3 on his banking card.

"I'm very fed up with living paycheck to paycheck! Why can't I become successful?"

You've been a part of those those questions, am I right?

Your very own success story is waiting to be written. You just need to take a leap of faith in YOURSELF.

Take Action Now!

Related Posts with Thumbnails