执行终端命令。

3

我希望能够从我的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.

关于“工作目录不存在”的消息:请检查调用setCurrentDirectoryPath:中的路径,是否存在任何拼写错误? - sjs
不要尝试使用ls命令,试试其他东西。 - eldos
2个回答

3

您需要以不同的方式设置工作目录:

[task setCurrentDirectoryPath:@"/Users/user/Desktop/project"];

然后将setLaunchPath:更改为指向实际可执行文件的位置:

[task setLaunchPath:@"/usr/bin/ant"];

0

嗯,你在这里尝试执行一个shell命令行。

先尝试更改目录(到/Users/blabla),然后再输入你的ant命令。我猜测程序试图找到一个名为cd /Users/user/Desktop/project/;ant的命令。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接