runtime.getruntime.exec 无法识别可执行文件
Posted
技术标签:
【中文标题】runtime.getruntime.exec 无法识别可执行文件【英文标题】:runtime.getruntime.exec does not recognize executable file 【发布时间】:2018-05-02 04:44:02 【问题描述】:我正在使用 Runtime.getRuntime().exec() 方法来运行“optimathsat.exe”文件。我的代码是这样的
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;
运行此代码后,控制台显示以下行
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
并在 bibi.txt 文件中显示以下错误
'optimathsat.exe' is not recognized as an internal or external command, operable program or batch file.
但是,当我将上述代码的几行复制到一个单独的项目(只有一个类)中,并在 winCommand 变量中替换生成的命令时,它可以完美运行。
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();
我猜第一个项目的配置不对,但我不知道该如何解决。
【问题讨论】:
使用ProcessBuilder
- 它支持更好的参数处理并且有一个“工作目录”属性来改变命令执行的位置
【参考方案1】:
您将cmd
用于两件事:
与其使用cmd
,不如使用Java 的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();
【讨论】:
以上是关于runtime.getruntime.exec 无法识别可执行文件的主要内容,如果未能解决你的问题,请参考以下文章
Android Runtime.getRuntime().exec
如何与 Runtime.getRuntime().exec(command) 程序进行交互?
在循环中运行“Runtime.getRuntime()。exec()”
Runtime.getRuntime.exec()执行java进程失败
无法从 Java 进程(Runtime.getRuntime().exec() 或 ProcessBuilder)读取 InputStream