2013-05-27 59 views
1

我有这样的代码:与选择Java运行C程序

try { 
    Process p = new ProcessBuilder(
      "/Applications/TorBrowser_en-US.app/Contents/MacOS/./tor", 
      "-f /Applications/TorBrowser_en-US.app/Library/filetctor/torrc") 
      .start(); 
    p.waitFor(); 
    int exitVal = p.exitValue(); 
    System.out.println("Process exitValue: " + exitVal); 
} catch (IOException e) { 
    System.out.println(e); 
} catch (InterruptedException e) { 
    System.out.println(e); 
} 

每次我执行它,我得到一个255 exitValue。该进程无法正常运行。

如果我运行只有程序:

Process p = new ProcessBuilder("/Applications/TorBrowser_en-US.app/Contents/MacOS/./tor").start(); 

过程正常运行。但我需要使用-f选项。

什么问题?我写错了吗?

+0

的'-f'和文件路径几乎可以肯定应该是独立的参数。 –

+0

您是否尝试自行运行该应用程序?它返回什么错误?尝试使用'getInputStream()'或'getErrorStream()'获取它。 – Djon

回答

3

每个参数应该是一个单独的字符串,并非全部位于一个空格分隔的字符串中。

the example in the documentation

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");

+0

Process p = new ProcessBuilder(“/ Applications/TorBrowser_en-US.app/Contents/MacOS /./tor”,“-f”,“/Applications/TorBrowser_en-US.app/Library/filetctor/torrc").start ();作品 –

+0

@MarcoMicheli太棒了!请点击左侧的复选标记,随时接受此答案。 – unwind