Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 365
YosiFZ Avatar asked Jun 29 '26 09:06

YosiFZ


1 Answers

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"];
like image 78
sjs Avatar answered Jul 01 '26 23:07

sjs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!