0

I want to invoke outlook from the command line (for various reasons) and wanted to know how I go about discovering the Path to the Outlook.exe file.

I'm pretty sure it's stored in the registry, but was wondering how to go about reading that from Java.

thanks

mmcdole
  • 91,488
  • 60
  • 186
  • 222
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238

4 Answers4

1

I found a Microsoft page that describes the procedure, just not in Java.

So I guess the question becomes how do I access the registry from java.

Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
  • I suggest editing both your question and title, or starting a new question if the nature of your question has changed. – mmcdole Dec 31 '08 at 18:26
1

I found this site that might be able to help you. It's a Java Registry wrapper, seems to have a lot of features but no idea how robust the implementation is.

Otis
  • 992
  • 3
  • 12
  • 22
0

Below is a solution modified slightly from a similar problem: https://stackoverflow.com/a/6194710/854664 Notice I'm using .pst instead of .xls

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ShowOutlookInstalled {
    public static void main(String argv[]) {
        try {
            Process p = Runtime.getRuntime()
                    .exec(new String[] { "cmd.exe", "/c", "assoc", ".pst" });
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String extensionType = input.readLine();
            input.close();
            // extract type
            if (extensionType == null) {
                outlookNotFoundMessage("File type PST not associated with Outlook.");
            } else {
                String fileType[] = extensionType.split("=");

                p = Runtime.getRuntime().exec(
                        new String[] { "cmd.exe", "/c", "ftype", fileType[1] });
                input = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String fileAssociation = input.readLine();
                // extract path
                Pattern pattern = Pattern.compile("\".*?\"");
                Matcher m = pattern.matcher(fileAssociation);
                if (m.find()) {
                    String outlookPath = m.group(0);
                    System.out.println("Outlook path: " + outlookPath);
                } else {
                    outlookNotFoundMessage("Error parsing PST file association");
                }
            }

        } catch (Exception err) {
            err.printStackTrace();
            outlookNotFoundMessage(err.getMessage());
        }


    }

    private static void outlookNotFoundMessage(String errorMessage) {
        System.out.println("Could not find Outlook: \n" + errorMessage);

    }
}
Community
  • 1
  • 1
dustydojo
  • 449
  • 5
  • 14
0

Using Otis' answer the following code does it nicely.

static String getOutlookPath() {
  // Message message = new Message();
  final String classID;
  final String outlookPath;

  { // Fetch the Outlook Class ID
    int[] ret = RegUtil.RegOpenKey(RegUtil.HKEY_LOCAL_MACHINE,   "SOFTWARE\\Classes\\Outlook.Application\\CLSID", RegUtil.KEY_QUERY_VALUE);
    int handle = ret[RegUtil.NATIVE_HANDLE];
    byte[] outlookClassID = RegUtil.RegQueryValueEx(handle, "");

    classID = new String(outlookClassID).trim(); // zero terminated bytes
    RegUtil.RegCloseKey(handle);
  }

  { // Using the class ID from above pull up the path
    int[] ret = RegUtil.RegOpenKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\Classes\\CLSID\\" + classID + "\\LocalServer32", RegUtil.KEY_QUERY_VALUE);
    int handle = ret[RegUtil.NATIVE_HANDLE];
    byte[] pathBytes = RegUtil.RegQueryValueEx(handle, "");

    outlookPath = new String(pathBytes).trim(); // zero terminated bytes
    RegUtil.RegCloseKey(handle);
  }

  return outlookPath;
}
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238