Sunday, April 13, 2008

Why not outlook?


This is my first post, and let me get to the issue straight away.

I was going through Java 2: The Complete Reference” by Herbert Schildt, there is this chapter on java.lang, in which there is a discussion of the methods of a class called Runtime.

A method called exec() is introduced which can call other programs. It seems similar to what happens in when you type notepad in Start ----> Run. In here, if you pass notepad as an argument to the method it would invoke the program. Here is the example program that the book gives:


public class ExecDemo {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Runtime r = Runtime.getRuntime();
Process p = null;

try {
p = r.exec("outlook");
p.waitFor();

} catch (Exception e) {
System.out.println("Error Opening outlook.");
}

System.out.println("Outlook Returned: " /*+ p.exitValue()*/);
}
}



It seems to work fine, but when I replace notepad with outlook.


p = r.exec("outlook");


It doesn’t work anymore. This is what I get in the output screen of my IDE (NetBeans):



So I commented off p.exitValue(), which is supposed to return 0 if the process (called) terminated normally or else it is supposed to return 1. The program degraded gracefully by calling on the exception:




The problem seems to be with the call to outlook. Does anyone know why it is unable to call outlook?

6 comments:

Kovica said...

Sorry, I'm not a Windows user, but with exec you should be able to run every program you can run from cmd.exe. So, if you start cmd.exe and type outlook and the program starts then you could try getting InputStream from the Process and read data from it. If outlook does not start in your cmd.exe then you have to find out the correct command to run Outlook. I'm sorry I don't know it.

Ramesh Subramanian said...

Kovica,

No need to be sorry... thanks for your comments!

Timothy said...

I don't think Outlook is part of your path, so you would have to use the fully qualified name which is
"C:\Program Files\Microsoft Office\OFFICE11\OUTLOOK.EXE" on my computer.
As Kovica said if you paste that into your command window and it executes you should be able to execute it from code.

Ramesh Subramanian said...

Timothy,

Thanks, Qualifying it by its path did open outlook!

Ramesh Subramanian said...

And I added "C:\Program Files\Microsoft Office\OFFICE11\" to my classpath... now outlook opens w/o having to type its entire address from the code... thanks for the help.

Timothy said...

you could achieve the same effect by adding "C:\Program Files\Microsoft Office\OFFICE11\" to your path.

if you add it you your path you would also be able to open it from the command prompt by just typing outlook.exe

Additionally you would be able to open any other executables that live in the OFFICE11 directory (ie powerpnt.exe) by just typing the executable name and not the fully qualified name