YosiFZ
YosiFZ

Reputation: 7900

Execute a terminal command

I want to run a terminal command from my objective-c project.

When I run it from the teminal I use :

cd /Users/user/Desktop/project/;ant release

now I used this in the Objective-C project:

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];

and after [task launch]; I get error:

launch path not accessible

Edit

I tried to use this command for check:

[task setCurrentDirectoryPath:@"/Users/user/Desktop/Czech/"];
[task setLaunchPath:@"/bin/ls"];

and it still give me a warning :

working directory doesn't exist.

Upvotes: 3

Views: 3072

Answers (2)

sjs
sjs

Reputation: 9310

You need to set the working directory in a different way:

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

Then change your setLaunchPath: call to point to the location of the actual executable:

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

Upvotes: 3

fge
fge

Reputation: 121710

Uhm, you try and execute a shell line here.

Try and change the directory first (to /Users/blabla) and then issue your ant command. My guess here is that the program tries to find a command named cd /Users/user/Desktop/project/;ant.

Upvotes: 0

Related Questions