0

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...

Klauss
  • 1
  • 1
  • 3
  • Your problem is perhaps only that the app has no standard output when launched from the Finder or Dock. Where did you expect to see the output of `print` ? Do you know if the Java task executed or not? Did you try to add more print statements to isolate the problem? – You could use NSLog() instead. – Martin R Jan 27 '15 at 15:22

1 Answers1

1

Check if you have your jar in your Bundle Resources.

Because if you use the NSBundle, you need to have the Jar also in your Bundle Resources.

If you don't have it in there, you have to add the JAR in your Xcode target under Build Phases to Copy Bundle Resources.

Christian
  • 22,585
  • 9
  • 80
  • 106
  • Yes I already added my jar into my bundle resources, and i can see it into the resources folder of my .app – Klauss Jan 27 '15 at 14:24
  • no there is no errors showing up because when i open my application in a terminal to see the standard output my application works well and i get no error. It's just when i open it from the app icon it doesn't work... – Klauss Jan 27 '15 at 14:39
  • got any solution? – LOG_TAG Mar 13 '19 at 09:23