4

I know a Java app can be bundled as a Mac app with the user-interface driven by Java Swing or JavaFX, and a Java runtime (JRE) bundled. But I want to build a native app in Xcode written in Swift with a Cocoa user-interface that calls upon a Java library to process some data.

I need to invoke the bundled JRE, call the desired library (JAR file), pass a reference to a file in the Mac’s storage (or else the file’s content as a large String in memory), and get back a string (XML or CSV or tab-delimited data).

This Question is similar to this Question but updated for Swift and the modern macOS and Java 8.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

1 Answers1

4

Unless you want all the pain involved in communicating directly in memory through something like JNI, the only way is to launch an external process using NSTask (for example) reading/writing a file or reading standard out.

If all the Java components are in your .app, you can directly reference the bundled JRE and program JAR.

/Applications/MyApp.app/.../java -jar /Applications/MyApp.app/.../MyAppJar.jar

Not sure how you are planning on bundling the JRE, since this will not be a Swing app. Maybe you would use appbundler to make a fake Swing .app, then just copy the file structure over to your native app.

If so, keep in mind that you will probably have to manually copy over bin/java, since appbundler doesn't included automatically.

Community
  • 1
  • 1
martinez314
  • 12,162
  • 5
  • 36
  • 63
  • should I embed java also in by app bundle? – Johnykutty Apr 01 '17 at 18:16
  • 1
    **Update:** [Java 9 is now modularized](https://en.wikipedia.org/wiki/Java_Platform_Module_System). See the [jlink](http://docs.oracle.com/javase/9/tools/jlink.htm) tool and [spec](http://openjdk.java.net/jeps/282), a [linker](https://en.wikipedia.org/wiki/Linker_%28computing%29) for Java. This means a smaller runtime size to be bundled with your app. Other link-time optimizations can be put in place as well for faster performance. – Basil Bourque Oct 15 '17 at 17:50
  • Any hope for doing the same in an iOS app? – AmitaiB Dec 20 '18 at 17:47
  • @BasilBourque any new way of doing this possible? using the latest java? thanks for the info – LOG_TAG Mar 13 '19 at 09:16
  • 1
    @LOG_TAG Yes, you can build an entire iOS app in Java, and distribute the app on the Apple App Store. Oracle has spun-off JavaFX as an open-source project now hosted and led by Gluon. See https://OpenJFX.io/. You can use the latest Java 11 & 12. – Basil Bourque Mar 13 '19 at 16:24
  • @BasilBourque that is the great info!, if we want to call java code from Objc/swift do you thing OpenJFX will reduce the efforts like Java JNI, old way of calling JVM inside Objc NSthread (looking for modern way for bridging objc/swift to java /jar files) https://github.com/SwiftJava/SwiftJava – LOG_TAG Mar 14 '19 at 04:36
  • 1
    @LOG_TAG No, nothing about JavaFX has to do with calling native code, it is a GUI toolkit. – Basil Bourque Mar 14 '19 at 05:01
  • 1
    @LOG_TAG [Java Native Access (JNA)](https://en.m.wikipedia.org/wiki/Java_Native_Access) is an easier alternative to JNI for calling native code from Java. But that is the opposite direction of this Question, which asks for calling Java code from native code. – Basil Bourque Mar 14 '19 at 05:07