执行终端命令
问题描述:
我想从我的objective-c项目运行终端命令。执行终端命令
当我从端子短运行它,我用:
cd /Users/user/Desktop/project/;ant release
现在我在Objective-C的项目中使用这样的:
NSTask *task = [NSTask new];
[task setLaunchPath:@"cd /Users/user/Desktop/project/;ant"];
[task setArguments:[NSArray arrayWithObjects:@"release", nil]];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task launch];
NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
[task waitUntilExit];
[task release];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog (@"got\n%@", string);
[string release];
和[task launch];
后,我得到错误:
launch path not accessible
编辑
我试图用这个命令检查:
[task setCurrentDirectoryPath:@"/Users/user/Desktop/Czech/"];
[task setLaunchPath:@"/bin/ls"];
,它仍然给我一个警告:
working directory doesn't exist.
答
你需要设置不同的方式工作目录:
[task setCurrentDirectoryPath:@"/Users/user/Desktop/project"];
然后改变你的setLaunchPath:
调用指向实际的可执行文件的位置:
[task setLaunchPath:@"/usr/bin/ant"];
答
嗯,你尝试在这里执行shell线。
尝试首先更改目录(至/Users/blabla
),然后发出您的ant
命令。我的猜测是程序试图找到一个名为cd /Users/user/Desktop/project/;ant
的命令。
关于`工作目录没有按't exist`消息:检查调用路径到`setCurrentDirectoryPath:`,是否有拼写错误? – sjs 2011-12-14 11:39:52