Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Run Excel using runtime.getRuntime().exec

try {
    Runtime.getRuntime().exec("excel C:\\file.xls");
} catch (IOException ex) {
    System.out.println(ex);
}

Doesn't work. I have to put the full path of excel.exe in order to work. How can I make it generic (For any Excel Folders/Versions)? When I run the same line from OS with Windows Run (Start --> Run) it works. Is there a code in Java to simulate Windows' Run command?

like image 867
Stefanos Kargas Avatar asked Sep 02 '26 13:09

Stefanos Kargas


2 Answers

Why don't you try with the Desktop class (api doc here) introduced in JDK6 that has the method

public void open(File file) throws IOException

which is documented as what you want to do:

Launches the associated application to open the file. If the specified file is a directory, the file manager of the current platform is launched to open it.

Of course this assumes that .xls extension is mapped by OS to Excel. Then you can go with

Desktop.getDesktop().open(new File("c:\\file.xls"));
like image 63
Jack Avatar answered Sep 04 '26 02:09

Jack


I use Runtime rt = Runtime.getRuntime().exec("cmd.exe /C start " + *filename* it works for me on Windows platforms

like image 26
Fabrice Khedadi Avatar answered Sep 04 '26 03:09

Fabrice Khedadi