Reputation: 1616
I am trying to execute a jar in my Cocoa app. My research tells me I should use NSTask
. However, I am only able to get system()
to work. E.g.:
// This works
system("cd /path/to ; /usr/bin/java -jar file.jar");
But this doesn't work:
// This does not work
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/java"];
[task setArguments:[NSArray arrayWithObjects: @"-jar", @"/path/to/test.jar", nil] ];
[task launch];
I get a class not found Java exception when I run my app because it cannot find its dependencies.
Upvotes: 0
Views: 1214
Reputation: 539735
There is no general error in your code, I tried it out myself.
I assume that you have defined a CLASSPATH environment variable in your .profile, .bashrc or whatever, that is required to execute the Java file.
system()
uses the shell to execute your command, and therefore the CLASSPATH is inherited by the java
command.
NSTask
on the other hand does not use the shell, so java
would not know about the CLASSPATH from your profile.
The solution would be to add @"-cp", @"<your class path>"
to the arguments.
UPDATE
As it turned out in the discussion, the problem in this case was not the class path, but the current working directory. Adding
[task setCurrentDirectoryPath:@"/path/to"]
solved the issue.
Upvotes: 5