从java代码调用可执行文件

时间:2015-02-09 12:14:02

标签: java exe

我正在尝试使用java代码中的参数运行可执行文件。在我希望运行我的exe的控制台中,我写道:>colorDescriptor 1.jpg --detector densesampling --ds_spacing 6 --ds_scales 1.2 --descriptor opponentsift --codebook CODEBOOK --output output.descr。当我尝试从java运行该可执行文件时,我使用以下代码:

    String[] cmd = { "colorDescriptor.exe" , "1.jpg", " --detector densesampling --ds_spacing 6 --ds_scales 1.2 --descriptor opponentsift --codebook CODEBOOK  ", "  --output output.descr"};
    Process process = Runtime.getRuntime().exec(cmd);        
    waitF(process);

然而,似乎没有正常工作,我遇到了以下问题:Warning: no output file to write to. Did you forget to specify --output?当我打电话给.exe时我做错了什么?

2 个答案:

答案 0 :(得分:2)

每个参数都应该是String [] cmd中的单独字符串; e.g:

String[] cmd = { "colorDescriptor.exe" , "1.jpg", "--detector", "densesampling", "--ds_spacing", "6", "--ds_scales", "1.2", "--descriptor", "opponentsift", "--codebook", "CODEBOOK", "--output", "output.descr"};

答案 1 :(得分:1)

你必须提供命令作为包含多个字符串的单个字符串或字符串数​​组,然后在try-catch块中包装,因为exec(...)抛出异常

Throws:
SecurityException - If a security manager exists and its checkExec method doesn't allow creation of the subprocess
IOException - If an I/O error occurs
NullPointerException - If command is null
IllegalArgumentException - If command is empty