• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to open the excel file in java program?

 
Kevin Pang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in windows environment.



java.io.IOException: Cannot run program ""D:\prog": CreateProcess error=193, %1 ???Ч?? Win32 ??ó
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at OpenExcel.main(OpenExcel.java:12)
Caused by: java.io.IOException: CreateProcess error=193, %1 ???Ч?? Win32 ??ó
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 4 more


appreciated.
 
Maneesh Godbole
Saloon Keeper
Posts: 11064
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to escape the characters properly.
It should be "d:\\\\prog file\\ 090422.xls"

Not UI related. Moving
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The space character in the path is the problem; use the Runtime.exec variant that takes a String[] as parameter. Also be sure to read the article by Daconta titled "When Runtime.exec() doesn't".
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem has a different cause: the Excel file is simply not an executable by itself. Therefore, you can't launch it as a program.

There are (using Windows) two ways of handling this:
- with rundll32.exe; I just keep forgetting the complete parameters
- using "cmd /c start xxx.xls"

The latter launches a hidden command window which executes "start xxx.xls". The "start" part causes Windows to use the default program, which usually is Excel.


No idea how this works in Unix; I think it all depends on the desktop environment used (Gnome, KDE, ...).

Edit: just tried it with a file with spaces, cmd does not like that. I did find the complete rundll32 parameters: http://forums.sun.com/thread.jspa?threadID=699829&tstart=285
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the spaces part would probably require wrapping in "" (i.e \"prog file\")

Runtime.getRuntime().exec("cmd /c start D:\\\"prog file\"\\090422.xls");

 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried that of course - no go. Even if I open the start command from another command window, and let the tab completion add the quotes - still no go. It's an issue of the start command itself.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just tested it like this on vista, opened OK (changing D: to C:)

Runtime.getRuntime().exec("cmd /c start C:\\\"prog file\"\\090422.xls");

 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, it only works if you put the quotes around the path parts that contain spaces. I tried quoting the entire path, as one would expect.

So the rundll32 solution is easier - it does the same* but only requires the quotes around the entire path, not each part.

* The main differences I found are:
1) rundll32 does not show an "open with" window if there is no program associated with the file
2) rundll32 does not show an error if the file does not exist


Edit: tried it some more, and if the file path contains spaces, "cmd /c start" needs quotes around everything after the drive specification. For instance C:\\\"prog file\\090422.xls\"". So if you use getAbsolutePath(), strip of the drive part, add quotes and add the drive part again, you're safe ;)
 
Kevin Pang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for replies.

this is ok:

public static void main(String[] args) {

try{
// Runtime.getRuntime().exec("\"D:\\prog file\\090422.xls\"");
String[] cmdarray=new String[]{"cmd.exe","/c","D:\\prog file\\090422.xls"};
Runtime.getRuntime().exec(cmdarray);
// or Runtime.getRuntime().exec("cmd /c start \"\" \"D:\\prog file\\090422.xls\"");

}catch(IOException e){
e.printStackTrace();
}

}
 
Bartek Myszkowski
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know You've solved the problem but.. isn't this easier:result is the same and You don't have to "play" with cmd here...
 
Maneesh Godbole
Saloon Keeper
Posts: 11064
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bartek Myszkowski wrote:I know You've solved the problem but.. isn't this easier:result is the same and You don't have to "play" with cmd here...


Yup. But only if you are on JDK1.6+
 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String[] cmdarray=new String[]{"cmd.exe","/c","D:\\prog file\\090422.xls"};
Runtime.getRuntime().exec(cmdarray);
// or Runtime.getRuntime().exec("cmd /c start \"\" \"D:\\prog file\\090422.xls\"");


I've one clarifications to seek. "/c" in the argument tells cmd to execute anything that comes after it. Can we have more than 2 commands to execute using the above exec method ? if yes, then how do we do that ?

Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read through the CMD help but it really says "command" - singular.

You can use a .bat or .cmd file that executes these commands, then call "cmd /c myfile.bat"; that's as far as I get.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bartek Myszkowski wrote:I know You've solved the problem but.. isn't this easier:result is the same and You don't have to "play" with cmd here...

It is damn easier............but can we control it in someway........
Suppose I open a word doc using Desktop, can I know whether the doc is still open or if it is closed............
Atleast Runtime returns a process for which we can wait for. Can we do something like that using Desktop.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you can't. That method does not block, nor does it return anything to identify the process with.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:No you can't. That method does not block, nor does it return anything to identify the process with.

Thanks Rob.
So that makes it clear that we have to use Runtime or ProcessBuilder even when we have similar methods in the Desktop class.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want any control over the process, then yes. Desktop.open() is really "fire and ignore".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic