-1

I want to check if JDK is installed.

I do not want to check if C:\Program Files\Java\jdk1.8.0_25 exists because the user may have an older version installed. I want to check if there is a directory inside of C:\Program Files\Java containing the string jdk.

Can I do that?

spongebob
  • 8,370
  • 15
  • 50
  • 83
jay
  • 29
  • 5
  • yes you can. but it might list irrelevant directories as well. – Codeek Nov 29 '14 at 13:26
  • @Codeek yes I know. But in my program, I do want to check for the JDK; as long as a version of the JDK exists, it's fine. – jay Nov 29 '14 at 13:29
  • The JDK can be anywhere the user puts it...so finding it could be a little task. – Trevor Nov 29 '14 at 13:39
  • @MrCoDeXeR Yes, but what's the point of installing it in a different location? Most people leave it at its default. – jay Nov 29 '14 at 13:42
  • @jay as a programmer you don't work under assumptions and most likeliness. you work on concrete requirements. – Codeek Nov 29 '14 at 13:53
  • 1
    This is a really flaky way to check if a program is installed, you will most certainly run into problems along the way. – Fred Nov 29 '14 at 14:04
  • How many standand users install the SDK none is my guess. Now on the other hand programmers will and the way your approaching it isnt going to work. My SDK is in another partition of the drive, you can assume thats the default but not always. Also just checking a directory doesnt mean its installed... – Trevor Nov 29 '14 at 14:07
  • 1
    http://stackoverflow.com/questions/1855937/how-to-detect-whether-java-runtime-is-installed-or-not Much better way. – Fred Nov 29 '14 at 14:09
  • @Fred No, but this will be mainly for standard users, you see. Basically, an API a lot of people use now requires a few command line args to get the final product. Those args require the JDK, and I'm making this program so that the normal users can use the API without thinking its too hard, and automating it for them. I will have a look at your way anyway, but for me it's not overly necessary. – jay Nov 29 '14 at 14:13
  • 1
    @jay It really doesn't matter which users they are, the level they are is irrelevant. Searching for a folder to determine something is installed is bad practice no matter who or what. – Fred Nov 29 '14 at 14:19
  • @Fred Alright, if I'm using your method about the registry, how do I check if it CAN open it? To me, those methods just look like they are opening a RegistryKey, but what if the RegistryKey doesn't exist? – jay Nov 29 '14 at 14:19
  • Is this for the Minecraft game too? Why does everybody want to make a Minecraft Launcher? :) – Visual Vincent Nov 29 '14 at 23:44
  • @VisualVincent Not for Minecraft :P – jay Nov 30 '14 at 08:48

4 Answers4

0

Yes.

Step 1: get a list of all directory names. Link

Step 2: check if one or more contain the searchstring. Link

Community
  • 1
  • 1
Johannes
  • 6,490
  • 10
  • 59
  • 108
  • not exactly helpful as I'm still a little new when it comes to VB. Do you have any code examples? – jay Nov 29 '14 at 13:31
  • I keep editing this post. I start out with a minimal post and then research the topic until my post seems helpful – Johannes Nov 29 '14 at 13:31
0
Dim Installed = (
    From i In My.Computer.FileSystem.GetDirectories("C:\Program Files\Java")
    Where i.ToLower Like "*jdk*").Any
CrimsonKing
  • 2,696
  • 1
  • 14
  • 11
0

A simple registry check. You would need to add error trapping etc.

Dim rk As RegistryKey
Dim sk As RegistryKey
Dim currentVerion As String


rk = Registry.LocalMachine

sk = rk.OpenSubKey("SOFTWARE\\JavaSoft\\Java Runtime Environment")
If sk Is Nothing Then
    'Does not exist (IE Could not open it).
Else
    currentVerion = sk.GetValue("CurrentVersion").ToString()
End If
Fred
  • 5,663
  • 4
  • 45
  • 74
-1

Straight coming from MSDN.

Try
    Dim dirPath As String = "your directory where JDK might be present" 
    Dim dirs As List(Of String) = New List(Of String)(Directory.EnumerateDirectories(dirPath))
    For Each folder In dirs
        ' Logic to check existence of word "JDK" from end
        ' If found break
    Next
Catch UAEx As UnauthorizedAccessException
    ' Error Handling
Catch PathEx As PathTooLongException
    ' Error Handling
End Try
spongebob
  • 8,370
  • 15
  • 50
  • 83
Codeek
  • 1,624
  • 1
  • 11
  • 20
  • This answer is essentially an implementation of the steps below. Quite useful. I think this is the sample code jay wanted. – Johannes Nov 29 '14 at 13:38
  • 1
    Indeed it is. hehehe. I hope they won't call it cheating :P Shall I delete the post? 0.0 – Codeek Nov 29 '14 at 13:40
  • @Codeek Hey, I'm using this code: http://pastebin.com/wfFeLkLC but for some reason the MsgBox is popping up twice. Any ideas? [I currently don't have JDK installed, so the logic is coming from the Else statement.) – jay Nov 29 '14 at 13:45
  • You have copied the code without worry of indentation. – spongebob Nov 29 '14 at 13:46
  • @jay don't use else in for. use a boolean variable that is set to true in case jdk is found and then break out of loop. then outside the for loop check if the boolean variable is true or flase. if it is jdk present if not then jdk is not present – Codeek Nov 29 '14 at 13:48
  • @Joiner. I was abt to edit it when screen showed up saying your reviews are left and i approved them :P – Codeek Nov 29 '14 at 13:50
  • The problem with this is that the string you are searching for could be in the base path and this will return a false positive. – Fred Nov 29 '14 at 13:59
  • I know. but I noticed that OP is targeting default java directory. Hence gave him that code. – Codeek Nov 29 '14 at 14:04