0

I have a table of data and i wanted an export function, this application run on web so i want to create a csv file and then offer (open or save as option) usual download options. Im using CSV writer at the moment.
The line in question here is Runtime.getRuntime().exec("export.csv"); give error listed at bottom How would i do this?

Here is the action tied to a button

Action exportData = new Action() {
    private static final long serialVersionUID = -7803023178172634837L;

    @Override   
    public void execute(UIContext uic, ActionEvent event) {

        try{       
            CSVWriter writer = new CSVWriter(new FileWriter("export.csv"), '\t');
            int i = 0;
            while (i < forExport.getTotalCount()){
                String[] entries = {
                    forExport.getSearchResults().get(i).getName().getGivenNames() + forExport.getSearchResults().get(i).getName().getSurname(),
                    forExport.getSearchResults().get(i).getId()
                };
                writer.writeNext(entries);
                i++;
            }  
            writer.close();
            Runtime.getRuntime().exec("export.csv");  
        }catch(Exception e){
            system.out.print(e);
        }
    }
};

getting error java.io.IOException: Cannot run program "export.csv": CreateProcess error=193, %1 is not a valid Win32 applicationDEBUG

NOTE: i do not want it to automatically open in excel, i want the option to save or open.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sean F
  • 2,352
  • 3
  • 28
  • 40

3 Answers3

1

I don't understand why you are exec-ing anything whatsoever. This is server code. You don't want to exec Excel at the server at all. You want to write the file back to the browser, along with a content-disposition header.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • indeed, exac was there as a way i had done similar things in the past not on server, i didn't think it was right. thanks. Do you know of any good examples of writeing the file back to the browser, along with a content-disposition header. – Sean F Oct 03 '12 at 05:23
0

You should try

Runtime.getRuntime().exec("excel.exe export.csv"); 

export.csv is not a valid windows command.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • java.io.IOException: Cannot run program "excel.exe": CreateProcess error=2, The system cannot find the file specifiedDEBUG – Sean F Oct 03 '12 at 04:55
  • Try with fixed path from program files from `Dos` prompt. It will be not in the path then. I am on Ubuntu otherwise I could have tried on my own. If it works on `Dos prompt` then it works with `exec` – Amit Deshpande Oct 03 '12 at 04:58
  • i don't want it automatically being opened in excel, i just want to download the csv file, i cannot use fixed paths as it is a web app and independant of a particular machine. – Sean F Oct 03 '12 at 05:04
  • 1
    Runtime.getRuntime().exec() will never help you to download a file. If you are writing web application then it is done differently. You can refer tutorial over [here](http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/) for the same. – Amit Deshpande Oct 03 '12 at 05:24
  • maybe not worded so well in the question. i didnt want to use exac only, i just hadnt done this and that is how i did non server side things in the past – Sean F Oct 03 '12 at 05:25
  • 1
    You can use this [How to download file from website- Java / Jsp](http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp/) to get to know how to do it. – Amit Deshpande Oct 03 '12 at 05:26
0

A .csv file is a comma separated value file which is used to keep data. and the parameter of the exec command is the command as string to open the .csv file. But in your command string you have not specified any program with which the runtime should try to open the .csv file.

As a .csv file is not executable, In the command string parameter specify the program name with ahich you want to open the ..csv file.

Debobroto Das
  • 834
  • 6
  • 16