NSTask启动路径不可访问。在Xcode中工作。显示在XCode之外的错误 [英] NSTask launch path not accessible. Works in Xcode. Error shown out of XCode

查看:456
本文介绍了NSTask启动路径不可访问。在Xcode中工作。显示在XCode之外的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的。有关于堆栈溢出的几个问题。 和此处 - > NSTask启动路径不可访问



但是他们在这一天的答案不清楚是什么问题。只有在阅读 NSTask不会收到$ PATH的问题后用户的环境(问题的标题是误导性的),并用这两个答案 NSTask不从用户环境中获取$ PATH 在Cocoa中找到可执行文件的位置,我实现了解决方案。


看起来这是关于设置NS
任务或用户的shell(例如〜/ .bashrc),以便NSTask看到正确的
环境($ PATH)。


解决方案:

  task setLaunchPath:@/ bin / bash]; 
NSArray * args = [NSArray arrayWithObjects:@ - l,
@ - c,
@which git,//假设git是您要运行的启动路径
nil];
[task setArguments:args];

但是,这假设用户的shell 总是 bash,其他。通过确定shell解决此问题。

  NSDictionary * environmentDict = [[NSProcessInfo processInfo] environment]; 
NSString * shellString = [environmentDict objectForKey:@SHELL];


Ok. There are several questions on stack overflow about this. This question was the only question comes closest to mines, but it uses notifications.

The code is very simple. Create a new empty Mac OSX project and just paste the following code in the applicationDidFinishLaunching: method. It supposed to get the path of any executable file (in this case GIT).

NSTask *aTask = [[NSTask alloc] init];
NSPipe *outputPipe = [NSPipe pipe];
NSPipe *errorPipe = [NSPipe pipe];

[aTask setStandardOutput: outputPipe];
[aTask setStandardError: errorPipe];
[aTask setArguments:[NSArray arrayWithObject:@"which"]];
[aTask setLaunchPath:@"/usr/bin/git"];

NSFileHandle *outputFileHandler = [outputPipe fileHandleForReading];
NSFileHandle *errorFileHandler = [errorPipe fileHandleForReading];

[aTask launch];
[aTask waitUntilExit];

// Task launched now just read and print the data
NSData *data = [outputFileHandler readDataToEndOfFile];
NSString *outPutValue = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

NSData *errorData = [errorFileHandler readDataToEndOfFile];
NSString *errorValue = [[NSString alloc] initWithData:errorData encoding:NSUTF8StringEncoding];

NSLog(@"Error value: %@",errorValue);
NSLog(@"Output Value: %@",outPutValue);

This code sets up two reading pipes and runs one command: which git.

If i run this in XCode i get this results corretly:

Error value: ""  
Output Value: /usr/bin/git

If i go to my build/Products/Debug folder and double click the executable file, i get this message printed on the console app:

Question: So, what is really the problem here? please just dont make an alternative solution... I also want to know what the problem is.. thanks.

解决方案

OK turns out the answer is on stack overflow, but its spread across different questions.

The question was asked here -> Commands with NSTask and here -> NSTask launch path not accessible as well

But their answers as of this date arent clear as to what the problem was. It's only after reading the question from NSTask not picking up $PATH from the user's environment (the question's title was misleading) and with these two answers NSTask not picking up $PATH from the user's environment and Find out location of an executable file in Cocoa that I realized the solution.

It looks like this is about setting up either NS Task or the user's shell (e.g., ~/.bashrc) so that the correct environment ($PATH) is seen by NSTask.

Solution:

[task setLaunchPath:@"/bin/bash"];
NSArray *args = [NSArray arrayWithObjects:@"-l",
                 @"-c",
                 @"which git", //Assuming git is the launch path you want to run
                 nil];
[task setArguments: args];

However this assumes the user's shell is always bash and it will fail for others. Solve this by determining the shell.

NSDictionary *environmentDict = [[NSProcessInfo processInfo] environment];
NSString *shellString = [environmentDict objectForKey:@"SHELL"];

这篇关于NSTask启动路径不可访问。在Xcode中工作。显示在XCode之外的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆