How to convert a jar file into an app for OSX

posted in: Crazy Belts, Development | 0

Note: we recommend to have a proper knowledge of OSX and Java. We’re not going to explain how to do certain things, because the article would be too long.

One of the most difficult tasks while developing Crazy Belts has been setting the game to OSX. There is little information and very few tools to do it; in fact, only one tool available to pack a jar file into an app. This was a mandatory step to publish our game inside and outside OSX App Store. In Jemchicomac, we used two process for this:

  • Gatekeeper: to publish outside the store
  • Sandbox: to publish inside the store

Gatekeeper prevents third party apps to be run in the system without Apple’s signature and verification. It’s conceived for those apps that come from another stores or websites not related to the App Store. User can disable Gatekeeper, but we consider that’s fair to demonstrate you’re a valid partner of Apple and you app is trustworthy.

On the other hand, Sandbox is a tool that encapsulates you app and check it: if it’s harmful, then Sandbox aparts it from the rest of the operative system. This is an essential requirement to publish in the Mac Store and it has a different signature than Gatekeeper.

Before all of this, as you have imagined, it’s necessary to convert our jar in an app, whatever our commercial goal in OSX is. We need the following tools:

  • JRE or JDK: you need Java in your computer. It doesn’t have to be Oracle’s Java Virtual Machine, but it’s the one we used.
  • Mac Developer account: essential to sign the app.

Given those ingredients, we can start creating our app. We need Java 7 (in our OSX system, we find only Java 6 as preinstalled). Once we install our JVM with Java 7, we must go to one of the jdk or jre installations. For instance: …/home/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk. We click on the icon, and if the active version of Java is SE6 we have to disable it , and check SE7.

Now you have to configure Java Home: http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/

Once all of this is done, we’re ready for the next step. We create a folder with all necessary thing to build our app (I called mine “build”).

  • A build.xml file with the information for Ant of Java to create our app
  • An empty info.plist file. We just create a file called this.
  • A folder containing appbundler -1.0ea.jar.
  • A dist folder with the jar file we want to convert.
  • Optionally, a icns file that we have previously worked in and contains a whole family of icons with different sizes, to embellish the app file.

Before going on, we have to warn you that all this work is made with the Terminal and we have to go to each directory when required. For instance, if my build folder is at the Desktop, in the Terminal I must go using

[bash]cd desktop/build.[/bash]

For me, build.xml is like this:

[xml]<?xml version="1.0" encoding="UTF-8"?>

<project name="CrazyBelts" default="default" basedir=".">

<target name="bundle">

<taskdef name="bundleapp"

classpath="lib/appbundler-1.0ea.jar"

classname="com.oracle.appbundler.AppBundlerTask"/>

<bundleapp

outputdirectory="dist"

name="Crazy Belts"

displayname="Crazy Belts"

identifier="com.company.crazybelts"

shortversion="1.3.0"

version="1.3.0"

icon="icon.icns"

mainclassname="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"

copyright="2014 Company"

applicationCategory="public.app-category.puzzle-games">

<classpath file="dist/CrazyBelts12FINAL.jar"/>

<runtime dir="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home"/>

<!– Workaround since the icon parameter for bundleapp doesn’t work –>

<option value="-Xdock:icon=Contents/Resources/${bundle.icon}"/>

<arch name="x86_64"/>

<arch name="i386"/>

</bundleapp>

</target>

</project>[/xml]

Let’see each entry:

[xml]<project name="CrazyBelts" default="default" basedir=".">[/xml]

We must change the name to our app name.

[xml]<target name="bundle">[/xml]

This is the name of our Ant task, meaning this is the one we should use when calling for Ant. In our example, once we’re set in the directory we type:

[bash]ant bundle[/bash]

Taskdef must match the label of the task we want to run. In classpath we type the directory where the AppBundler is located. Finally, we leave classname without changes.

[xml]<bundleapp

outputdirectory="dist"

name="Crazy Belts"

displayname="Crazy Belts"

identifier="com.company.crazybelts"

shortversion="1.3.0"

version="1.3.0"

icon="icon.icns"

mainclassname="org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader"

copyright="2014 Company"

applicationCategory="public.app-category.puzzle-games">[/xml]

The main block of build.xml:

  • Output directory: we can put here the jar to pack (in our case, we did this), and it’ll also contain the app when it’s created.
  • name: the name of the app
  • display name: the name as displayed
  • identifier: it must match an App ID registered in your Mac Developer account. This registration can be done afterwards, and it’s only necessary if you’re going to distribute in the Mac Store, not for third parties.
  • Shortversion: the game version
  • Icon: this is the icon we mentioned before, in the root of the build folder.
  • Mainclassname: this is a very important entry. We must indicate the class that runs the app. In our case, we used Eclipse and we did spend so much time setting the main class  of the app, while this IDE actually used his own Leader class when creating the exe jar.
  • Copyright: our national copyright
[xml]<runtime dir="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home"/>

<!– Workaround since the icon parameter for bundleapp doesn’t work –>

<option value="-Xdock:icon=Contents/Resources/${bundle.icon}"/>

<arch name="x86_64"/>

<arch name="i386"/>[/xml]

Only runtime.dir is important for us, we don’t touch the rest. This entry is optional and if we don’t include it in our app it won’t have an embedded jdk. Good news are that doesn’t require much space; bad news are that if the user doesn’t have the correct jdk or jre, the app won’t run, so it’s poorly advisable. This includes the path where our jdk is located; if we don’t know it and we’re using Eclipse, we can go to Preferences and check the jre.

After that, we must go to the Terminal and type:

[bash]ant bundle.[/bash]

A package with .app extension will be created, and it will contain our .jar

The final part is signing the app. In our example, we must use another way apart from Xcode. Everything will be done using Terminal.

We’re going to start with the Gatekeeper version, the easier one. To sign an app and be accepted by this technology we must create a certificate with a Team Agent account. Be very careful with this: to publish outside the App Store you must create a Developer ID Application Certificate or Developer ID Installer Certificate, depending on whether we want to only sign the app as it is or to pack it into an installer (that also has to be signed).

To create a certificate, we always have to go to the development console in the section “Certificates, Identifiers & Profiles”. Once created and added to the Keychain (check carefully we have the private key associated to the public one), we just set into the app directory and type:

[bash]codesign -v -f -s "Developer ID Application"  "myapp.app"[/bash]

If we get an error message, we type the following command:  export CODESIGN_ALLOCATE=”/Applications/Xcode.app/Contents/Developer/usr/bin/codesign_allocate”

And if everything goes right, we must see this: signed bundle with Mach-O universal (i386 x86_64) [com.company.myapp]

Leave a Reply