Start java program from Objective C code
-
I'm trying to make a pretty simple frontend program that's able to start up a java program when it's run, but haven't yet mastered OC. Someone suggested that I try using a console command (code below), but I haven't been able to make that work yet. Is there an easier way to do this, or does any one here know where my code has gone wrong?
int main(int argc, const char * argv[]) {
NSFileManager \*filemgr; NSString \*currentpath; filemgr = \[\[NSFileManager alloc\] init\]; currentpath = \[filemgr currentDirectoryPath\]; NSPipe \*pipe = \[NSPipe pipe\]; NSFileHandle \*file = pipe.fileHandleForReading; NSTask \*task = \[\[NSTask alloc\] init\]; task.launchPath = \[currentpath stringByAppendingString:@"/java"\]; task.arguments = @\[@"-jar", @"PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\]; task.standardOutput = pipe; \[task launch\]; NSData \*data = \[file readDataToEndOfFile\]; \[file closeFile\]; NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\]; NSLog (@"grep returned:\\n%@", grepOutput); return 0;
}
When I run this, I get the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' I know that the current path is accessible, though... so I'm kind of stumped here. Any help would be much appreciated. Thanks!
-
I'm trying to make a pretty simple frontend program that's able to start up a java program when it's run, but haven't yet mastered OC. Someone suggested that I try using a console command (code below), but I haven't been able to make that work yet. Is there an easier way to do this, or does any one here know where my code has gone wrong?
int main(int argc, const char * argv[]) {
NSFileManager \*filemgr; NSString \*currentpath; filemgr = \[\[NSFileManager alloc\] init\]; currentpath = \[filemgr currentDirectoryPath\]; NSPipe \*pipe = \[NSPipe pipe\]; NSFileHandle \*file = pipe.fileHandleForReading; NSTask \*task = \[\[NSTask alloc\] init\]; task.launchPath = \[currentpath stringByAppendingString:@"/java"\]; task.arguments = @\[@"-jar", @"PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\]; task.standardOutput = pipe; \[task launch\]; NSData \*data = \[file readDataToEndOfFile\]; \[file closeFile\]; NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\]; NSLog (@"grep returned:\\n%@", grepOutput); return 0;
}
When I run this, I get the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' I know that the current path is accessible, though... so I'm kind of stumped here. Any help would be much appreciated. Thanks!
-
As a guess because what you think is the current path for the executable is not in fact the path where the jar actually is.
-
I'm trying to make a pretty simple frontend program that's able to start up a java program when it's run, but haven't yet mastered OC. Someone suggested that I try using a console command (code below), but I haven't been able to make that work yet. Is there an easier way to do this, or does any one here know where my code has gone wrong?
int main(int argc, const char * argv[]) {
NSFileManager \*filemgr; NSString \*currentpath; filemgr = \[\[NSFileManager alloc\] init\]; currentpath = \[filemgr currentDirectoryPath\]; NSPipe \*pipe = \[NSPipe pipe\]; NSFileHandle \*file = pipe.fileHandleForReading; NSTask \*task = \[\[NSTask alloc\] init\]; task.launchPath = \[currentpath stringByAppendingString:@"/java"\]; task.arguments = @\[@"-jar", @"PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\]; task.standardOutput = pipe; \[task launch\]; NSData \*data = \[file readDataToEndOfFile\]; \[file closeFile\]; NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\]; NSLog (@"grep returned:\\n%@", grepOutput); return 0;
}
When I run this, I get the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' I know that the current path is accessible, though... so I'm kind of stumped here. Any help would be much appreciated. Thanks!
-
task.launchPath = [currentpath stringByAppendingString:@"/java"];
Are you sure that the java executable is in that path? Printing out the full path at that point will tell you.
Ah, I do see what I was doing there! I was fundamentally misunderstanding how java was being referenced. I almost have it working at this point! Here's the code as it stands now:
int main(int argc, const char * argv[]) {
NSFileManager *filemgr;
NSString *currentpath;filemgr = \[\[NSFileManager alloc\] init\]; currentpath = \[filemgr currentDirectoryPath\]; NSPipe \*pipe = \[NSPipe pipe\]; NSFileHandle \*file = pipe.fileHandleForReading; NSTask \*task = \[\[NSTask alloc\] init\]; task.launchPath = @"/usr/bin/java"; task.arguments = @\[@"-jar", \[currentpath stringByAppendingString:@"/PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\]; task.standardOutput = pipe; \[task launch\]; NSData \*data = \[file readDataToEndOfFile\]; \[file closeFile\]; NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\]; NSLog (@"grep returned:\\n%@", grepOutput); return 0;
}
The only remaining problem is that the "-jar" argument seems to be ignored at this point. If I apply it by itself, Java recognizes it as an argument, but otherwise, it is only accepting "PolyGlot.jar". Is there something that I'm not doing correctly there? Thanks for the help so far!
-
Ah, I do see what I was doing there! I was fundamentally misunderstanding how java was being referenced. I almost have it working at this point! Here's the code as it stands now:
int main(int argc, const char * argv[]) {
NSFileManager *filemgr;
NSString *currentpath;filemgr = \[\[NSFileManager alloc\] init\]; currentpath = \[filemgr currentDirectoryPath\]; NSPipe \*pipe = \[NSPipe pipe\]; NSFileHandle \*file = pipe.fileHandleForReading; NSTask \*task = \[\[NSTask alloc\] init\]; task.launchPath = @"/usr/bin/java"; task.arguments = @\[@"-jar", \[currentpath stringByAppendingString:@"/PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\]; task.standardOutput = pipe; \[task launch\]; NSData \*data = \[file readDataToEndOfFile\]; \[file closeFile\]; NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\]; NSLog (@"grep returned:\\n%@", grepOutput); return 0;
}
The only remaining problem is that the "-jar" argument seems to be ignored at this point. If I apply it by itself, Java recognizes it as an argument, but otherwise, it is only accepting "PolyGlot.jar". Is there something that I'm not doing correctly there? Thanks for the help so far!
-
I don't know Objective-C, but I suspect that the first argument should be the same as
task.launchPath
, i.e the program name. That would then comply with the format of theargv
array, as set by the shell when invoking programs from the command line.The
task.launchPath
is the full path of the program to be run. Out of curiosity, I tried dropping " -jar" onto the end, but that made it fail when it looked for the executable "java -jar" rather than the executable "java" with the argument "-jar". What's strange is that the "-jar" argument works correctly so long as it's the only one... Ultimately, the error I'm getting from Java at this point is "Could not find or load main class
". This typically appears if you try to run a .jar file without the -jar argument. Also, just for the sake of sanity, I'll specify that I can run
"java -jar PolyGlot <FILENAME>"
from the console, and it runs fine. -
The
task.launchPath
is the full path of the program to be run. Out of curiosity, I tried dropping " -jar" onto the end, but that made it fail when it looked for the executable "java -jar" rather than the executable "java" with the argument "-jar". What's strange is that the "-jar" argument works correctly so long as it's the only one... Ultimately, the error I'm getting from Java at this point is "Could not find or load main class
". This typically appears if you try to run a .jar file without the -jar argument. Also, just for the sake of sanity, I'll specify that I can run
"java -jar PolyGlot <FILENAME>"
from the console, and it runs fine.DraqueD wrote:
I can run
"java -jar PolyGlot <FILENAME>"
from the console, and it runs fine.And I think those are the fields that you need in your
arguments
array. The last parameter you specify should beargv[1]
as a string (%s), not a character (%c), as that is the filename passed in to your program. Something like:task.launchPath = @"/usr/bin/java"; task.arguments = @\[task.launchPath, @"-jar", \[currentpath stringByAppendingString:@"/PolyGlot.jar"\], \[NSString stringWithFormat:@"%s", argv\[1\]\]\];
-
I don't know Objective-C, but I suspect that the first argument should be the same as
task.launchPath
, i.e the program name. That would then comply with the format of theargv
array, as set by the shell when invoking programs from the command line. -
I'm trying to make a pretty simple frontend program that's able to start up a java program when it's run, but haven't yet mastered OC. Someone suggested that I try using a console command (code below), but I haven't been able to make that work yet. Is there an easier way to do this, or does any one here know where my code has gone wrong?
int main(int argc, const char * argv[]) {
NSFileManager \*filemgr; NSString \*currentpath; filemgr = \[\[NSFileManager alloc\] init\]; currentpath = \[filemgr currentDirectoryPath\]; NSPipe \*pipe = \[NSPipe pipe\]; NSFileHandle \*file = pipe.fileHandleForReading; NSTask \*task = \[\[NSTask alloc\] init\]; task.launchPath = \[currentpath stringByAppendingString:@"/java"\]; task.arguments = @\[@"-jar", @"PolyGlot.jar"\], \[NSString stringWithFormat:@"%c", argv\]\]; task.standardOutput = pipe; \[task launch\]; NSData \*data = \[file readDataToEndOfFile\]; \[file closeFile\]; NSString \*grepOutput = \[\[NSString alloc\] initWithData: data encoding: NSUTF8StringEncoding\]; NSLog (@"grep returned:\\n%@", grepOutput); return 0;
}
When I run this, I get the error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'launch path not accessible' I know that the current path is accessible, though... so I'm kind of stumped here. Any help would be much appreciated. Thanks!
I think there is an problem in executable path..Check out that