HOW TO OPEN WORD DOCUMENT FROM JAVA?
hi everyone! Can you please help me to come with this problem? Let me to set my main target I want to achieve:
1) In my program user should first press a button;
2) Then already created Word Document should be opened (I DO NOT NEED to open that Word Document in Java or something like JField). Just simply it should be opened as a Microsoft Word Document.
I DO NEED YOUR HELP! PLEASE HELP ME....!
[421 byte] By [
SmiLeJoNa] at [2007-10-2 22:44:30]

Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \""+fileName+"\"");
Complicated Windows only solution...
Use:
Runtime.getRuntime().exec("cmd /c \""+fileName+"\"");
it's platform independent.
Also - "Search Forums" is very usefull feature. This is very frequently asked question: "How to open XXX file from Java?".
I doubt if its platform independent ........
It's more platform independent then rundll32 usage anyway :)
Hi,and if you want something more sofisticated, look at this project: http://jakarta.apache.org/poi/index.htmlL.P.
Running cmd using the exec is also a windows only solution, there is no "more" or "less" it that solution compared to the rundll.
Since word is a windows format its pretty obvious that the application is ran under windows platform.
If you are running on a different platform (maybe using open office you can open word documents, i'm not sure) you might want to use the JDIC library (https://jdic.dev.java.net/) which enables you to open/edit/browse such files using the default viewer/editor/browser of the system you are working on.
Look at the org.jdesktop.jdic.desktop.Desktop object for these methods.
You can also find the Desktop object in J2SE6 but its still in beta.
How can I specify the locaiton of file. IT does not work if I do like
"C:\Documents and Settings\B Sharma\Desktop\plan.doc"
I got the idea but I could solve the problem of spaces in folder's name.
In visual basic, I could actually set the applicaiton path to the location of vb file and put the file to be opened in the very folder and I could open by just specifying hte file.
How can I do that sort of thing in java. Any help is greatly appreciated.
thanks
right now I working with teh following code: However I am longing for something more efficient and less error prone.
String fileName = "C:\"Docume~1" + "\\" + "CATBTD~1"+"\\"+"Desktop" + "\\" + "menu"+ "\\"+"plan.doc";
try
{
Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \""+fileName);
}
catch (IOException e1)
{ e1.printStackTrace(); }
}
what you should do is add " before and after the file name that way spaces will be accepted.Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \""+fileName+"\"");
Now Java 6 it final and one can use the Desktop class
try
{
Desktop.getDesktop().open( new File("c:/word.doc") );
}
catch ( Exception /* IOException, URISyntaxException */ e )
{
e.printStackTrace();
}
Christian Ullenboom | tutego
thanks all. I got it running