?
Solved

URGENT: How to open MS Word using Java

Posted on 2009-02-23
5
Medium Priority
?
577 Views
Last Modified: 2013-12-29
Hi i need my program to open a new MS Word and i found the following code in previous questions. It compiles and run just fine. No errors no exceptions at all. The problem is not opening anything
package test;
 
import java.io.IOException;
 
/**
* FileHanlder opens a native file using the windows shell
*/
public class FileHandler {
 
public static void main(String[] args) {
FileHandler fileHandler = new FileHandler();
fileHandler.displayFile("WINWORD");
}
 
protected void displayFile(String fileName) {
 
// create a string buffer for building our command
StringBuffer cmd = new StringBuffer();
 
// prepare the command to be executed using the windows shell
cmd.append(" rundll32 url.dll,FileProtocolHandler ");
cmd.append(" file://");
cmd.append(fileName);
 
try {
 
Process p = Runtime.getRuntime().exec(cmd.toString());
try {
int exitCode = p.waitFor();
if (exitCode != 0) {
// command failed .. do error handling
}
} catch (InterruptedException ie) {
ie.printStackTrace();
}
 
} catch (IOException e) {
System.out.println("Could not execute command: " + cmd);
}
 
}
}

Open in new window

0
Comment
Question by:nvlachos
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 23714412
Try replacing

>>
cmd.append(" rundll32 url.dll,FileProtocolHandler ");
cmd.append(" file://");
cmd.append(fileName);
>>

with

cmd.append("cmd.exe /C start ");
cmd.append(fileName);

Open in new window

0
 

Author Comment

by:nvlachos
ID: 23714435
Cheers mate!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23714448
:-)
0
 

Author Comment

by:nvlachos
ID: 23714462
What if i want to open an existing MS Word file instead of a new one?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 23714527
Just pass an existing file as fileName. Please delete your new Q - it's not necessary
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question