0

例外

/usr/sbin/asterisk -rx "dongle sms dongle0    km FROM> some address TO> some address 20 kms. Please respond in 2 minutes, ok, yes, 1 or book. Else ignore"
java.io.IOException: Cannot run program "/usr/sbin/asterisk -rx "dongle sms dongle0 " (in directory "."): java.io.IOException: error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:488)
    at java.lang.Runtime.exec(Runtime.java:610)
    at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:58)
    at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:254)
    at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:319)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:160)
    at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:147)
    at Test.main(Test.java:33)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:164)
    at java.lang.ProcessImpl.start(ProcessImpl.java:81)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:470)
    ... 7 more

代码

public static void main(String[] args) {
        CommandLine commandLine = new CommandLine("/usr/sbin/asterisk -rx \"dongle sms dongle0 ");
        DefaultExecutor executor = new DefaultExecutor();           
        String cmd[] = new String[8] ;
        cmd[0] = "" ; // phonenumber
        cmd[1] = "" ; // distancefrom customer 
        cmd[2] = "km FROM>" ;
        cmd[3] = " some address " ;
        cmd[4] = " TO>" ;
        cmd[5] = " some address ";
        cmd[6] = "20" ;
        cmd[7] =  "kms. Please respond in 2 minutes, ok, yes, 1 or book. Else ignore\"";

        try {               
            for (int i = 0 ; i < cmd.length ; i++)
                commandLine.addArgument(cmd[i], false) ;
            System.out.println(commandLine.toString());
            executor.execute(commandLine);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

我试过了

CommandLine commandLine = new CommandLine("/bin/sh -c echo | sudo /usr/sbin/asterisk -rx \"dongle sms dongle0 "); 我得到了 /bin/sh 完全相同的异常

CommandLine commandLine = new CommandLine("/bin/dash -c echo | sudo /usr/sbin/asterisk -rx \"dongle sms dongle0 "); 我得到了 /bin/dash 完全相同的异常

4

1 回答 1

0

修复它,使用 CommandAction 和 CommandResponse

ManagerConnection c = null;
    CommandAction commandAction;
    CommandResponse response = null;
    try {
        c = new ManagerConnectionFactory("127.0.0.1", 5038, "username", "password")
                .createManagerConnection();
        c.login();
        commandAction = new CommandAction(command);
        response = (CommandResponse) c.sendAction(commandAction);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    } catch (AuthenticationFailedException e) {
        e.printStackTrace();
    }
    for (String line : response.getResult()) {
        System.out.println(line);
    }

manager.conf 应该有以下内容

[username]
secret=password
read=system,call,log,verbose,agent,command,user,originate
write=system,call,log,verbose,agent,command,user,originate
permit=127.0.0.1/255.255.255.0
于 2013-07-04T13:12:04.590 回答