2010-08-17 28 views
0

这里是我的代码:我看不到NSTask输出 - Objective-C的JAVA

NSTask *setupTask = [NSTask new]; 
[setupTask setLaunchPath:@"/bin/sh"]; 
[setupTask setArguments:[NSArray arrayWithObject:@"/applications/jarvis/brain/server.sh"]]; 
[setupTask setCurrentDirectoryPath:@"/"]; 
NSPipe *outputPipeSetup = [NSPipe pipe]; 
[setupTask setStandardInput:[NSPipe pipe]]; 
[setupTask setStandardOutput:outputPipeSetup]; 
[setupTask launch]; 

NSTask *aliceTask = [NSTask new]; 
[aliceTask setLaunchPath:@"/usr/bin/java"]; 
[aliceTask setArguments:[NSArray arrayWithObjects:@"-classpath", @"/applications/jarvis/brain/", @"-Xms64m", @"-Xmx128m", @"org.alicebot.server.net.AliceServer", nil]]; 

NSPipe *aliceInputPipe = [NSPipe pipe]; 
[aliceTask setStandardInput:aliceInputPipe]; 
NSPipe *aliceOutputPipe = [NSPipe pipe]; 
[aliceTask setStandardOutput:aliceOutputPipe]; 

[aliceTask launch]; 

NSMutableString *outputString = [NSMutableString string]; 
while ([outputString rangeOfString:@"Jarvis>"].location == NSNotFound) { 
    [outputString appendString:[[[NSString alloc] initWithData:[[aliceOutputPipe fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding] autorelease]]; 
} 

,但outputString返回任何内容,它得到的卡在while循环。这里是server.sh文件:

echo Starting Jarvis Program D. 
ALICE_HOME=. 
SERVLET_LIB=lib/servlet.jar 
ALICE_LIB=lib/aliceserver.jar 
JS_LIB=lib/js.jar 

# Set SQL_LIB to the location of your database driver. 
SQL_LIB=lib/mysql_comp.jar 

# These are for Jetty; you will want to change these if you are using a different http server. 
HTTP_SERVER_LIBS=lib/org.mortbay.jetty.jar 

PROGRAMD_CLASSPATH=$SERVLET_LIB:$ALICE_LIB:$JS_LIB:$SQL_LIB:$HTTP_SERVER_LIBS 
java -classpath $PROGRAMD_CLASSPATH -Xms64m -Xmx128m org.alicebot.server.net.AliceServer $1 
+0

疯了!你为什么从编译目标c调用Java? – 2010-08-17 19:16:13

+0

如果我要解释它需要几个小时!:D但简单地说,我想为ALICE AI制作一个客观的GUI。 AIML解释器是用java编写的。在终端中,我只是启动.sh文件,它加载了ALICE并为我提供了一个用于userinput和Alice响应的字段。我稍后在我的代码中解析这个。 – objectiveccoder001 2010-08-17 20:02:26

回答

0

您可以尝试只使用一个NSTask调用来处理server.sh输出:SH -c“源server.sh | sed“/ Jarvis>/q”'(参见shstring)。在这个简化的代码示例中,server.sh(shfile)只包含一个cat(1)命令,用于将test.file的内容写入stdout。也许你可以将从Objective-C调用的java命令添加到shstring中?

#import <Foundation/Foundation.h> 

/* 

See: 
http://stackoverflow.com/questions/3505942/i-cant-see-the-output-of-nstask-objective-c-java 

compile with: 
gcc -Wall -O3 -x objective-c -fobjc-exceptions -framework Foundation -o test test.c 

./test 

# create test.file 
n=50 
jot -b 'line' $n | nl -w 10 > test.file 
echo "Jarvis>" | nl -w 10 -v $((n+1)) >> test.file 
jot -b 'line' 19 | nl -w 10 -v $((n+2)) >> test.file 

ls -lh test.file 
tail -n 100 test.file 

# create server.sh 
echo ' 
cat test.file 
' > server.sh 

sh server.sh 

./test 

*/ 


int main(int argc, const char *argv[]) 
{ 

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    NSString *shfile = @"server.sh"; 

    NSString *shstring = [NSString stringWithFormat: 
      @"%@ %@ %@", 
      @"source", 
      shfile, 
      @"| /usr/bin/sed '/Jarvis>/q'" 
    ]; 

    NSTask *sh = [NSTask new]; 
    NSPipe *pipe = [NSPipe new]; 
    [sh setLaunchPath:@"/bin/sh"]; 
    [sh setArguments:[NSArray arrayWithObjects: @"-c", shstring, nil ]]; 
    [sh setCurrentDirectoryPath:@"."]; 
    //[sh setStandardInput:[NSFileHandle fileHandleWithNullDevice]]; 
    [sh setStandardError:[NSFileHandle fileHandleWithNullDevice]]; 
    [sh setStandardOutput:pipe]; 
    [sh launch]; 

    NSData *data = [[[sh standardOutput] fileHandleForReading] readDataToEndOfFile]; 
    NSString *string = [[[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding] autorelease]; 

    NSLog(@"\n\n%@\n", string); 

    [pool release]; 

    return 0; 

} 
+0

你能举一个java实现的例子吗? – objectiveccoder001 2010-08-18 23:29:52