I am currently teaching a student on how to code a game in Java on a Mac. GameCenter is a service provided by Apple that lets one see their game stats on a leaderboard and lets them play online. Does anyone know if it's possible to implement GameCenter in Java or if it's even possible to publish a Java app on the Mac App Store?
-
Not without some JNI/JNA. You could start by looking at the [Game Centre for Developers](https://developer.apple.com/game-center/) – MadProgrammer Jan 18 '14 at 21:33
2 Answers
The Basics
You would need to translate parts of your Java application over to Objective-C, which is what OS X and iOS support.
Fortunately in the last couple of years there has been a decent amount of progress and attention in terms of tools, techniques, and methods that can help you do this. There's even an open-source tool/runtime that can take Java code and turn it into Objective-C:
It's fairly popular, and can probably set you in the right direction.
EDIT: I've added the word parts above since it was apparently unclear to Basil Bourque, and/or possibly anyone else who may have taken the word translate as having to re-write your entire app in objective-c.
-
1Wow! That's amazing! I never knew that there were converters! Thanks for the info! I'd up vote this if I had enough reputation. Until then, please take this comment as my sincere thanks. – alexsmbaratti Jan 18 '14 at 21:53
-
Or, you could look at using a cocoa bridge of some type, like [Rococoa](http://code.google.com/p/rococoa/) – MadProgrammer Jan 18 '14 at 21:55
-
@alexsmbaratti, You're welcome! — I'm glad it's helpful to you, cheers :) – l'L'l Jan 18 '14 at 22:03
Java-Based Mac App
The answer by l'L'l is not entirely correct. You do not need to translate your Java code to Objective-C. You can ship a Java-based app in the Apple Mac App Store.
Apple changed their policies to now allow Java-based apps for sale in the Mac App Store. This applies only to Mac apps, as far as I know, not for iOS. There is no Java implementation released that runs on iOS as far as I know. Sun demonstrated a prototype once publicly, but never released anything for iOS afaik.
See my answer to a similar question. The successful app Moneydance is an example, as noted on this answer.
Nutshell
- You create a thin Objective-C app that wraps your Java app, using Xcode.
Other people have provided templates for this minimal code. - Your Mac app must include a JVM (Java Virtual Machine).
In other words, your Mac app cannot access a JVM that may already be installed on the user’s Mac. - You may use Swing or JavaFX for presenting a user interface.
Both Apple’s and Oracle’s implementations of Java ship with extensions to Swing for a Mac look and feel. The latest implementations from Oracle also include JavaFX.
Oracle provides these instructions on how to package your app for the Mac App Store.
GameCenter
I do not know the details of accessing GameCenter from the Java-based part of your game. You may need to write some Objective-C in order to interact with Apple's GameCenter libraries/framework.
As noted by MadProgrammer (comment on question), your Java code may interact with Objective-C code by using either:
- JNI – Java Native Interface
Bundled with Java platform, by Oracle/Sun. - JNA – Java Native Access
A third-party library. Becoming popular as a simpler alternative to JNI. See this question for comparison of JNA to JNI.
Xojo
By the way, another non-Objective-C platform for shipping apps in the Mac App Store is Xojo, formerly known as RealBasic. A very slick set of tools with a fully object-oriented programming language. Popular with both beginning programmers and professionals.

- 1
- 1

- 303,325
- 100
- 852
- 1,154
-
By "translate" I did not mean "re-write in it's entirety", so i've edited my answer to clarify. And are you saying that java applications on OS X or iOS wouldn't need at least some portions to be written in Objective-C or Objective C++? Just curious, since I was under the impression some of it needed to be if you're using the Apple frameworks. Also, thanks for adding the nice summary of techniques, good information definitely :) – l'L'l Jan 19 '14 at 04:45
-
@l'L'l No, you needn’t rewrite any of your Java code as far as I know. But note I've not yet shipped such an app yet, so I have no experience, only study. You just need the most basic of an Xcode project with just enough Objective-C code to launch the JVM and your classes. Other folks have provided that example code as I mentioned. Also, I revised my answer to make clear that only the *Mac* App Store allows Java apps, not the iOS app store. – Basil Bourque Jan 19 '14 at 22:56
-
@l'L'l As for reaching out to utilize Apple's Cocoa (or Carbon) libraries, yes then you'll need to write some Objective-C code and use JNI/JNA to interface with your Java code. But for many kinds of apps, the Java platform is complete. You have access to the file system, networking, collections, [Joda-Time](http://www.joda.org/joda-time/), and all the other great Java libraries. Swing lets you build a normal-looking Mac-style user interface. And now JavaFX lets you build wilder-looking user interfaces. – Basil Bourque Jan 19 '14 at 23:00