Opening a MS Word file with java

Hi,

I'm trying to write an action performed method in java that will open a specified MS Word file.

I've tried the following (as part of my gui), but when I click the button I get lots of errors.

public void actionPerformed(ActionEvent e) {
            try{
                  
      String command = "\"C:\\Documents and settings\\All users\\Start Menu\\Programs\\Microsoft Office\\Office\\WINWORD.EXE" + "c:\\testingFile.doc" ;
                                                            
      Process p = Runtime.getRuntime().exec(command);
      int exitValue = p.waitFor();
      }catch (Exception x) {
         x.printStackTrace();
      }


Office is stored at:  C:\Documents and Settings\All Users\Start Menu\Programs\Microsoft Office
and the file at: C:\testingFile.doc

any ideas guys?

Thanks
LFC1980Asked:
Who is Participating?
 
CEHJCommented:
Or

String[] command = { "cmd.exe", "/c", "start", "c:/testingFile.doc" };

You need to execute that in a separate thread really though, or it could block your GUI thread
0
 
CEHJCommented:
>>WINWORD.EXE"

should have a space:

WINWORD.EXE "
0
 
CEHJCommented:
Actually it should be

"\"C:\\Documents and settings\\All users\\Start Menu\\Programs\\Microsoft Office\\Office\\WINWORD.EXE\" " +  "c:\\testingFile.doc" ;
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
CEHJCommented:
Easiest would be

"C:/DOCUME~1/ALLUSE~1/STARTM~1/Programs/MICROS~1/Office/WINWORD.EXE " + "c:/testingFile.doc";
0
 
LFC1980Author Commented:
I've tried all of these - none seem to work :(
0
 
LFC1980Author Commented:
tell a lie, the last one works!
Thanks :)
0
 
CEHJCommented:
>>none seem to work

Meaning?
0
 
CEHJCommented:
:-)

That's probably the best one. You don't need

>>int exitValue = p.waitFor();
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.