I'm trying to execute a jar file from my swift application. I use the code bellow:
let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource("MyJar", ofType: "jar")!
let task = NSTask()
task.launchPath = "/usr/bin/java"
task.arguments = ["-jar", path, arg]
let pipe = NSPipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)!
print(output)
It works well when i execute the application from xcode but when i launch the application from the app icon it doesn't work and i don't get why...
Edit : So I've put some NSLog() into my code :
let bundle = NSBundle.mainBundle()
let path = bundle.pathForResource("AudioTagger", ofType: "jar")!
NSLog("%@", "jar path : \(path)")
let task = NSTask()
task.launchPath = "/usr/bin/java"
task.arguments = ["-jar", path, xml]
let pipe = NSPipe()
task.standardOutput = pipe
let errorPipe = NSPipe()
task.standardError = errorPipe
task.launch()
task.waitUntilExit()
NSLog("%@", "Task is finished")
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)!
let errorData = pipe.fileHandleForReading.readDataToEndOfFile()
let errorOutput: String = NSString(data: errorData, encoding: NSUTF8StringEncoding)!
NSLog("%@", output)
NSLog("%@", errorOutput)
And i get this into the console :
16:41:09 MyApp: jar path : /Users/User/Library/Developer/Xcode/DerivedData/MyApp-fbeddkvghwjpjpgvrkrfzbjxjvse/Build/Products/Release/MyApp.app/Contents/Resources/MyJar.jar
16:41:09 MyApp: Task is finished
16:41:14 Console: Failed to connect (_consoleX) outlet from (NSApplication) to (ConsoleX): missing setter or instance variable
The standard error seems to be empty, so i still don't get it...