package net.sf.jabref.gui.desktop.os;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;

import net.sf.jabref.gui.externalfiletype.ExternalFileType;
import net.sf.jabref.gui.externalfiletype.ExternalFileTypes;
import net.sf.jabref.preferences.JabRefPreferences;

import static net.sf.jabref.preferences.JabRefPreferences.USE_PDF_READER;

public class Windows implements NativeDesktop {
    private static String DEFAULT_EXECUTABLE_EXTENSION = ".exe";

    @Override
    public void openFile(String filePath, String fileType) throws IOException {
        Optional<ExternalFileType> type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType);

        if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) {
            openFileWithApplication(filePath, type.get().getOpenWithApplication());
        } else {
            // quote String so explorer handles URL query strings correctly
            String quotePath = "\"" + filePath +"\"";
            new ProcessBuilder("explorer.exe", quotePath).start();
        }
    }

    @Override
    public String detectProgramPath(String programName, String directoryName) {
        String progFiles = System.getenv("ProgramFiles(x86)");
        if (progFiles == null) {
            progFiles = System.getenv("ProgramFiles");
        }
        if ((directoryName != null) && !directoryName.isEmpty()) {
            return Paths.get(progFiles, directoryName, programName + DEFAULT_EXECUTABLE_EXTENSION).toString();
        }
        return Paths.get(progFiles, programName + DEFAULT_EXECUTABLE_EXTENSION).toString();
    }

    @Override
    public void openFileWithApplication(String filePath, String application) throws IOException {
        new ProcessBuilder(Paths.get(application).toString(), Paths.get(filePath).toString()).start();
    }

    @Override
    public void openFolderAndSelectFile(String filePath) throws IOException {
        new ProcessBuilder("explorer.exe", "/select,", filePath).start();
    }

    @Override
    public void openConsole(String absolutePath) throws IOException {
        ProcessBuilder process = new ProcessBuilder("cmd.exe", "/c", "start");
        process.directory(new File(absolutePath));
        process.start();
    }

    @Override
    public void openPdfWithParameters(String filePath, List<String> parameters) throws IOException {
        String[] params = (String[]) parameters.toArray();
        String[] command = new String[parameters.size() + 1];
        command[0] = Paths.get(JabRefPreferences.getInstance().get(USE_PDF_READER)).toString();
        for( int i = 1; i < command.length; i++) {
            command[i] = parameters.get(i-1);
        }
        new ProcessBuilder(command);
    }
}

@@ -2427,67 +2427,8 @@
 n %7B%0A
-        String%5B%5D params = (String%5B%5D) parameters.toArray();%0A
     
@@ -2481,17 +2481,17 @@
 ize() + 
-1
+2
 %5D;%0A     
@@ -2505,16 +2505,23 @@
 and%5B0%5D =
+ %22%5C%22%22 +
  Paths.g
@@ -2578,32 +2578,39 @@
 DER)).toString()
+ + %22%5C%22%22
 ;%0A        for( i
@@ -2637,16 +2637,20 @@
 d.length
+ - 1
 ; i++) %7B
@@ -2674,16 +2674,23 @@
 and%5Bi%5D =
+ %22%5C%22%22 +
  paramet
@@ -2701,16 +2701,23 @@
 get(i-1)
+ + %22%5C%22%22
 ;%0A      
@@ -2712,32 +2712,92 @@
 %22%5C%22%22;%0A        %7D%0A
+        command%5Bcommand.length-1%5D = %22%5C%22%22 + filePath + %22%5C%22%22;%0A
         new Proc

Add filePath and quotes to the command for windows