简体   繁体   中英

runtime.getruntime.exec does not recognize executable file

I am using Runtime.getRuntime().exec() method for running 'optimathsat.exe' file. My code is like

public boolean runOptimathSat() throws InterruptedException {
    boolean runSucceed = false;
    smtInputFileDirectory = getInputDirectory();
    txtOutputFileDirectory = getOutputDirectory();
    optimathsatDirectory = getOptimathSatDirectory();
    if ((smtInputFileDirectory != null) && (txtOutputFileDirectory != null)
            && (optimathsatDirectory != null)) {

        if (isWindows()) {
            String winCommand;
            winCommand = "cmd /c cd " + optimathsatDirectory + " && optimathsat.exe -opt.print_objectives=True -opt.output_format=old -optimization.card_constr_encoding=2 -optimization.dpll.search_strategy=1 -preprocessor.toplevel_propagation=False -preprocessor.simplification=0 < " + smtInputFileDirectory + " > " + txtOutputFileDirectory + " 2>& 1";
            System.err.println("COMMAND: "+winCommand);
            try {
                Process p = Runtime.getRuntime().exec(winCommand);
                p.waitFor();
                runSucceed = true;  
            } catch (IOException e) {
                e.printStackTrace();
            }
    return runSucceed;}

After running this code, it shows the below line in console

COMMAND: cmd /c cd "C:\Users\Karencom\OptiMathSAT\optimathsat-1.5.1-windows-64-bit-mingw\bin" && optimathsat.exe -opt.print_objectives=True -opt.output_format=old -optimization.card_constr_encoding=2 -optimization.dpll.search_strategy=1 -preprocessor.toplevel_propagation=False -preprocessor.simplification=0 < "C:/Users/Karencom/runtime-New_configuration/tetest/Optimathsat/bibi.smt2" > "C:/Users/Karencom/runtime-New_configuration/tetest/Optimathsat/bibi.txt" 2>& 1

and shows below error in bibi.txt file

'optimathsat.exe' is not recognized as an internal or external command, operable program or batch file.

However, when I copy some lines of the above code in a separate project(which has just one class), and replace the generated command in winCommand variable, it works perfectly.

import java.io.IOException;
public class Test {
public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    try {
        String winCommand="cmd /c cd"+ " \"C:\\Users\\Karencom\\OptiMathSAT\\optimathsat-1.5.1-windows-64-bit-mingw\\bin\" && optimathsat.exe -opt.print_objectives=True -opt.output_format=old -optimization.card_constr_encoding=2 -optimization.dpll.search_strategy=1 -preprocessor.toplevel_propagation=False -preprocessor.simplification=0 < \"C:/Users/Karencom/runtime-New_configuration/tetest/Optimathsat/sensen.smt2\" > \"C:/Users/Karencom/runtime-New_configuration/tetest/Optimathsat/sensen.txt\" 2>& 1";
        Process p = Runtime.getRuntime().exec(winCommand);
        p.waitFor();    
        System.err.println("COMMAND: "+winCommand);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

I guess that first project's configuration is not right but I do not know how can I resolve it.

You are using cmd for 2 things:

  • Setting the current directory
  • Redirecting input and output

Rather than using cmd for that, you should use Java's ProcessBuilder .

String[] winCommand = {
        "optimathsat.exe",
        "-opt.print_objectives=True",
        "-opt.output_format=old",
        "-optimization.card_constr_encoding=2",
        "-optimization.dpll.search_strategy=1",
        "-preprocessor.toplevel_propagation=False",
        "-preprocessor.simplification=0"
};
Process p = new ProcessBuilder(winCommand)
        .directory(new File(optimathsatDirectory))        // "cd " + optimathsatDirectory
        .redirectInput(new File(smtInputFileDirectory))   // "< " + smtInputFileDirectory
        .redirectOutput(new File(txtOutputFileDirectory)) // "> " + txtOutputFileDirectory
        .redirectErrorStream(true)                        // 2>& 1
        .start();
p.waitFor();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM