jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Open a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Open a file" Watch "Open a file" New topic

Open a file

nisha Sham

Joined: Jun 12, 2002
Posts: 19
How can I Open a Word or Excel or any other file using java application

Joined: Jun 03, 2002
Posts: 2
use can use the process class.
eg. Process p = Runtime.getRuntime().exec("<executble file name iec:\word.exe>","<document file name>");
The process class is the best way to invoke a file from word. Like this u can use the same class to call any exe file.
real eg:
Process p = Runtime.getRuntime().exec("c:\word.exe","test.doc");
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
I tried this
Process p = Runtime.getRuntime().exec("E:\Program Files\Microsoft Office\Office\winword.exe","test.doc");
and I get an Illegal escape character
Do you have an idea?

By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
nisha & rajarajan,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardners!

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Process p = Runtime.getRuntime().exec("E:\\Program Files\\Microsoft Office\\Office\\winword.exe","test.doc");
It still won't work. Can you figure out why?
[ June 12, 2002: Message edited by: Dirk Schreckmann ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If all you want to do is open and read/write a file then you can use the FileReader and FileWriter classes.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
nisha Sham

Joined: Jun 12, 2002
Posts: 19
I tried with
Process p = Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\Office\\winword.exe" ,"C:\\backup\\testt.doc");
When run the application following error prompt as
java.io.IOException: CreateProcess: C:\Program Files\Microsoft Office\Office\winword.exe,C:\backup\testt.doc error=2
at java.lang.Win32Process.create(Native Method)

showing that there is some error in Win32Process class....
Please help !
Mohan Panigrahi
Ranch Hand

Joined: Sep 28, 2001
Posts: 142
I tried doing :
Runtime.getRuntime().exec("C:\\Program Files\\Microsoft Office\\Office\\winword.exe C:\\hello.doc");
and it works. The hello.doc has to be there already existing.
Can someone throw some light on :
(a) How to use the .exec(String, String[]) version. ( name, value pair thing mentioned in javadoc is confusing )
(b) How to create a new document ( ie when hello.doc is not existing )
nisha Sham

Joined: Jun 12, 2002
Posts: 19

I Succeeded . Thanks Mohan
I agree. Here's the link: http://aspose.com/file-tools
subject: Open a file
It's not a secret anymore!