【问题标题】:Trying to run NSTask but getting an error尝试运行 NSTask 但出现错误
【发布时间】:2011-07-02 04:43:42
【问题描述】:

我有一个独立的(第 3 方)应用程序,我正在尝试使用“kick”命令启动它。我已经设置了我的 ~/.bash_profile 和 /etc/bashrc 文件,这样我就可以在终端窗口中输入 kick [command] 并且它运行良好。所以我假设我的所有设置都正确。

当我尝试使用 NSTask 时,问题就来了。

基本上我正在做的是创建两个可变数组,kickBuild 和 kickOut。一个用于组装命令(它是一串标志),一个用于与 NSTask 一起使用。我采用 kickBuild 并将其转换为以空格分隔的字符串,然后将其作为对象添加到第二个数组中。

所以我的命令应该类似于 "kick -i /path/to/input/file.ext -as 2 -g 2.2" 等。如果我在终端窗口中输入它效果很好。

kickBuild = [[NSMutableArray alloc] initWithCapacity:100];
kickOut = [[NSMutableArray alloc] initWithCapacity:2]; // Thinking that this should be "kick" and then "-i /path/to/input/file.ext -as 2 -g 2.2"
kickPath = [kickLocationTextField stringValue]; // This is just the path to my kick executable. NOT /bin/sh. Is that correct?
NSString *tempKick = [kickBuild componentsJoinedByString: @" "];
[kickOut addObject:tempKick];
[NSTask launchedTaskWithLaunchPath:kickPath arguments:kickOut];

当我构建我的应用程序并运行此代码时,我收到此错误...

dyld:库未加载:build/darwin_x86_64/gcc_opt_dynamic/core/libai.dylib

引用自:/Users/Gene/Kick/Kick-3.3.4.0/bin/kick

原因:找不到图片

这是 kickOut 的 NSLog… kick -i /Users/Gene/Test_Main.0001.ext -r 960 540 -as 2 -g 2.2 -v 5 -dp

这是我做错了吗?还是踢的问题?

如何使用一些基本的终端任务测试 NSTask 以确保我正确使用 NSTask?

kickBuild = [[NSMutableArray alloc] initWithCapacity:5];
kickOut = [[NSMutableArray alloc] initWithCapacity:2];
kickPath = @"/bin/sh";
[kickBuild addObject:@"-c"]; // Do I need this?
[kickBuild addObject:@"ls"];
[kickBuild addObject:@"~/Desktop"];
NSString *tempKick = [kickBuild componentsJoinedByString: @" "];
[kickOut addObject:tempKick];
[NSTask launchedTaskWithLaunchPath:kickPath arguments:kickOut];

如果我在没有 @"-c" 的情况下运行它,我会得到:/bin/sh: ls ~/Desktop: No such file or directory

感谢任何帮助。

谢谢一百万

【问题讨论】:

    标签: objective-c nstask


    【解决方案1】:

    执行 NSTask 时不会读取您的环境设置。

    几年前我问过this question

    【讨论】:

    • 感谢 Abizem 的回复。我知道 ~/.bash_profile 仅适用于交互式终端,但 /etc/bashrc 不是对所有东西都有效吗?互动还是非互动?
    • 尝试在 ~/.MacOSX/environment.plist 中设置 Paths 将被读取。
    【解决方案2】:

    这是一个简单的 NSTask 测试,应该适合你(注意:arguments:(NSArray *)arguments):

    /*
    
    gcc -Wall -O3 -x objective-c -fobjc-exceptions -framework Foundation -o nstask nstask.m
    
    ./nstask
    
    http://stackoverflow.com/questions/5081846/trying-to-run-nstask-but-getting-an-error
    
    
    launchedTaskWithLaunchPath:arguments:
    
    Creates and launches a task with a specified executable and arguments.
    
    + (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments
    
    
    */
    
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char *argv[])
    {
    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSTask *task = [NSTask new];
    
    NSMutableArray *kickBuild = [[NSMutableArray alloc] initWithCapacity:5];
    //NSMutableArray *kickOut = [[NSMutableArray alloc] initWithCapacity:2];
    NSString *kickPath = @"/bin/ls";
    
    //[kickBuild addObject:@"/bin/ls"]; // Do I need this?
    [kickBuild addObject: [@"~/Desktop" stringByExpandingTildeInPath]];
    
    task = [NSTask launchedTaskWithLaunchPath: kickPath arguments: kickBuild];
    [task waitUntilExit];
    
    
    [pool release];
    
    return 0;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 2021-05-26
      • 2021-07-23
      • 2013-09-17
      • 2017-04-04
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多