-1

I'd like my program to scan the user's hard drive for java.exe.

I was considering using a for-loop and then going through each directory and then check if the directory names match the ones I would set up and then check in those for the java exe, but I'm sure there is a much more efficient way of doing so.

Any ideas on how to approach this?

Edit:

I've gone ahead and done some foreach loops, but it isn't going too well. I'm probably missing something:

// Scan for Java executable (java.exe)
        foreach (String dir in Directory.GetDirectories("C:/"))
        {
            if (dir == "Program Files")
            {
                foreach (String _dir in Directory.GetDirectories(dir)) {
                    if (_dir == "Java")
                    {
                        foreach (String javaDir in Directory.GetDirectories(_dir))
                        {
                            if (javaDir == "jre7")
                            {
                                foreach (String binDir in Directory.GetDirectories(javaDir)) {
                                    if (binDir == "bin")
                                    {
                                        foreach (String file in Directory.GetFiles(binDir))
                                        {
                                            if (file == "java.exe")
                                            {
                                                javaExe = file;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
tshepang
  • 12,111
  • 21
  • 91
  • 136
SimonC
  • 1,547
  • 1
  • 19
  • 43

2 Answers2

1

Do you only want to see if java.exe is there?

File.Exists(@"C:\Program Files\Java\jre7\bin\java.exe")

You might need additional logic to look in the Program Files (x86) folder if needed. Also this will obviously fail if the user installed java somewhere else.

If you want to know where is java, take a look at this other Stack Overflow post.

Community
  • 1
  • 1
dee-see
  • 23,668
  • 5
  • 58
  • 91
1

You are looking for java.exe. So Firstly you should look for directory where oracle always install it's java.exe which should be at

C:\Windows\System32

to limit your search and then search in other directories.

         var paths = new List<string>
        {
           Environment.SystemDirectory,
           Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)+@"\java",
           Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)+@"\java",
           Environment.GetFolderPath(Environment.SpecialFolder.Programs)+@"\java"
           //C:\
           //rest
        };
        foreach (string path in paths)
        {
            var ser = Search("java.exe", path);

            if (!string.IsNullOrEmpty(ser))
            {
                if (File.Exists(ser))
                {
                    javaExe = ser;
                    break;
                }
            }
        }

Search function:

 private static string Search(string pattern, string root)
    {
        if (!Directory.Exists(root))
        {
            return string.Empty;
        }
        var paths = new Queue<string>();
        paths.Enqueue(root);
        while (paths.Count > 0)
        {
            root = paths.Dequeue();
            string[] temp = Directory.GetFiles(root, pattern);
            foreach (string t in temp)
            {
                return t;
            }
            temp = Directory.GetDirectories(root);
            foreach (string t in temp)
            {
                paths.Enqueue(t);
            }
        }

        return string.Empty;
    }

Also it's preferred to use this search method to avoid Access permissions.

Note: You could find java.exe in more than one place.

Community
  • 1
  • 1
Alyafey
  • 1,455
  • 3
  • 15
  • 23