1

I set up an NSNotification for NSFileHandleReadCompletionNotification.

I set up the standard I/O with two separate pipes.

NSPipe * input  = NSPipe.new;
NSPipe * output = NSPipe.new;

[serverTask setStandardInput:input];
[serverTask setStandardOutput:output];

I launch a NSTask executing a Java jar, and start reading the data.

[[serverTask.standardOutput fileHandleForReading] readInBackgroundAndNotify];

and I continuously read the data and append the data in an NSTextView if it is new data:

- (void)serverLogHasChanged:(NSNotification *)notification
{
    [[serverTask.standardOutput fileHandleForReading] readInBackgroundAndNotify];

    NSData * newData = [notification.userInfo objectForKey:NSFileHandleNotificationDataItem];

    if (newData != nil && availableData != newData)
    {
        NSMutableString * serverLogString = [NSMutableString stringWithString:serverLog.string];

        [serverLogString appendString:[NSString.alloc initWithData:newData encoding:NSUTF8StringEncoding]];
        [serverLog setString:serverLogString];
        [serverLog.enclosingScrollView.contentView scrollPoint:NSMakePoint(0, NSMaxY(serverLog.enclosingScrollView.contentView.bounds))];
    }

    newData = availableData;
}

However, I'm getting strange output into the NSTextView

enter image description here

Those ">" characters should be lines of actual output, but instead the output ends up in Xcode's console.

In other words, the console is printing out the output instead of my NSPipe which is printing out only the indication of new lines of the output.

evdude100
  • 437
  • 4
  • 18
  • Hmm, Mine Craft Wrapper? And did you try setting the output of the task to `[NSFileHandle fileHandleWithStandardOutput]`? – Richard J. Ross III Feb 01 '12 at 15:41
  • Yup. If I do that I get nothing in the NSTextView and now those characters in the console: `174 recipes 27 achievements > 10:46:49 [INFO] Starting minecraft server version 1.1 > 10:46:49 [INFO] Loading properties > 10:46:49 [INFO] Starting Minecraft server on localhost:25565 >` etc... – evdude100 Feb 01 '12 at 15:48
  • 2
    Any chance the output you are trying to catch is going to standard error? If so you need to capture that as well. – CRD Feb 01 '12 at 16:37
  • Oh wow.. Thanks CRD, It makes zero sense but it was located under Standard Error. Thanks! – evdude100 Feb 01 '12 at 18:38
  • @CRD If you want to copy your answer in the comments to an actual answer post to this question you can get points and close the question.. – evdude100 Feb 01 '12 at 18:46
  • evdude100, any chance you might share your NSTask arguments list? I'm having trouble launching a java session using NSTask that doesn't result in a broken VM. More specifically, "Unrecognized option: -jar foo.jar" – MikeyWard Apr 05 '12 at 05:25

1 Answers1

4

As has been commented above, it's a case of adding catching the standard error output. I've had a similar issue recently. I solved it by doing the following:

NSPipe *pipe = [NSPipe pipe];
[javaTask setStandardOutput:pipe];
[javaTask setStandardError:pipe];
NSFileHandle *fileHandleForReading = [pipe fileHandleForReading];

[javaTask launch];

NSData *result = [fileHandleForReading readDataToEndOfFile];

NSString *output = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];

Thanks, CRD for your help.

djbp
  • 714
  • 8
  • 24