17

Is there a way to convert a Java application into a Mac OS X executable app?

I use NetBeans to develop in Java, and I'd like to "pack" the "dist" folder into an app (just for convenience)

wkl
  • 77,184
  • 16
  • 165
  • 176
Barranka
  • 20,547
  • 13
  • 65
  • 83
  • I think you're asking about [JAR Bundler](https://developer.apple.com/library/mac/#documentation/Java/Conceptual/Jar_Bundler/About/About.html#//apple_ref/doc/uid/TP40000884-CH202-BAJCICAA). – wkl Jun 14 '12 at 16:51
  • 1
    @birryree Not exactly... when I build the project, NetBeans generates a ".jar" file and a "lib" folder, which contains the required libraries for the application... what I'd like to know is if there's a way to pack the full "dist" folder into an app, in order to distribute the full application (including required libraries) in one package, and to make this package executable in Mac OS X – Barranka Jun 14 '12 at 17:28
  • Possible duplicate: http://stackoverflow.com/questions/2695214/convert-jar-to-an-osx-executable – Asif Jun 14 '12 at 18:10
  • 1
    @Barranka: If you want to deploy a Java app on OS X, you'll want to involve `JarBundler` (and thus a Mac) in your build process somehow. While it's possible to make an app bundle using lower-level tools, it's probably just less convenient. – millimoose Jun 14 '12 at 18:10
  • 1
    I just google `jar to app converter` it results in bundles of apps and related blogs..[here](http://www.bartbusschots.ie/blog/?p=296) is one. – Asif Jun 14 '12 at 18:13
  • 1
    [Install4J](https://www.ej-technologies.com/products/install4j/overview.html) also works fine. Free for open source projects. – koppor Sep 18 '16 at 23:42
  • Modern tooling: [*JEP 282: jlink: The Java Linker*](https://openjdk.org/jeps/282) and [*JEP 392: Packaging Tool*](https://openjdk.org/jeps/392). Learn the [*Java Platform Module System*](https://en.wikipedia.org/wiki/Java_Platform_Module_System) – Basil Bourque Mar 30 '23 at 19:57

8 Answers8

18

Use the Apple Java Extensions and its Guide

The Apple Java Extensions contains a very complete development guide with information on the deployment of Java applications on Mac OS X and the production of application bundles. It also introduces other aspects of the Apple Java Extensions, like the support for integration with the standard Mac OS X UI.

Other references:

haylem
  • 22,460
  • 3
  • 67
  • 96
  • 3
    Unfortunately this guide is now in the retired library even though Apple published a bug fix release of Java 6 in October 2013. Any help? – Gene Dec 22 '13 at 22:18
  • @Gene: as far as I can tell, the second link in my first paragraph is still valid when it comes to creating a deployable application. The Java Extensions documentation does seem to have gone AWOL though, or to mention we're in legacy territory. But that's only for the integration APIs. – haylem Dec 27 '13 at 17:51
  • Thanks. That is a nice article. A significant addition is that Apple won't put a Java app in their store unless the entire jre is bundled with it. The Java 7 jre is huge: over 60 megabytes. And getting all the paths right is fiddly. I just found that there actually is a jre 6 available that works with Mavericks. Have to see if I can find a build procedure for this to get the bundle size down. – Gene Dec 27 '13 at 18:30
11

There is a library that let's you package your Java app
Packr: https://github.com/libgdx/packr

Packages your JAR, assets and a JVM for distribution on Windows (ZIP), Linux (ZIP) and Mac OS X (.app), adding a native executable file to make it appear like the app is a native app.

It can even minimize the JRE for you.

Andrejs
  • 26,885
  • 12
  • 107
  • 96
6

jar2app

Packr is a great tool, but at the time I found that I wanted something "easier to use", so jar2app was born. I know this is an old question but perhaps other people might find this program easier to use than other alternatives. If they don't, there's a direct reference in the FAQ to other alternatives (such as Packr).

Jorl17
  • 361
  • 3
  • 5
4

You can use javapackager tool to build the application and wrap it in into an installer, the following commands show how to convert a jar file into a bundle file:

commands

mkdir -p package/macosx
cp Test.icns package/macosx
jdk=$(/usr/libexec/java_home)
$jdk/bin/javapackager -deploy -native dmg \
   -srcfiles Test.jar -appclass package.Test -name Test \
   -outdir deploy -outfile Test -v
cp deploy/bundles/Test-1.0.dmg installer.dmg
ls -l
open installer.dmg

To change the application icon and more info MacJava.

Molham
  • 165
  • 1
  • 8
4

As of JDK14 there is also jpackage (JEP-392), currently promoted from incubation phase to a production-ready feature.

Example usage that worked with an swt app (assumes all the required jar files reside in the files folder; you can also provide a custom resource folder with --resource-dir):

jpackage --type dmg \
 -i files \
 -n Bigly \
 --main-class com.biglybt.ui.Main \
 --main-jar BiglyBT.jar \
 --java-options -XstartOnFirstThread \
 --mac-package-name BiglyBt \
 --icon app.icns \
 --verbose

For the full option list use:

jpackage --help

WARNING: It's probably debatable whether it's a bug or not, and maybe it will be addressed in a future release, but in the current version, if you specify the input folder as -i ., and do not provide a custom destination with --dest then jpackage will bundle everything in the current folder... including the bundle it just created, i.e. it goes recursive on itself when . is both the input folder and the output :D .

ccpizza
  • 28,968
  • 18
  • 162
  • 169
3

Since some of the links in the accepted answer are no longer available or suitable in 2020, 8 years from the question was asked, I would like to share my findings that I confirmed working today.

There is a tool, javapackager, shipped with java, can package java application on Windows, Linux, macOS for you.

Here is the official manual: https://docs.oracle.com/javase/8/docs/technotes/tools/unix/javapackager.html

(Also there are other helpful tools here: https://docs.oracle.com/javase/8/docs/technotes/tools/ )


For packaging a mac application, I used this command:

javapackager \
    -deploy \
    -native image \
    -srcdir ./csv-encrypt-tool-mac \
    -srcfiles csv-encrypt-tool.jar \
    -srcfiles dict \
    -srcfiles config \
    -srcfiles log \
    -outdir ./dist \
    -outfile csv-encrypt-tool \
    -appclass some.package.CsvEncToolApp \
    -name "csv-encrypt-tool" \
    -title "csv-encrypt-tool" \
    -nosign \
    -v \
    -BjvmOptions=-Xmx4096m \
    -BmainJar=csv-encrypt-tool.jar \
    -Bicon=icon.icns

And this is the explanation:

-deploy \                                       # Assembles the application package for redistribution with sys JRE
-native image \                                 # Build a .app file. If you want a .dmg, use -native dmg
-srcdir ./csv-encrypt-tool-mac \                # directory where my jar and resource files in
-srcfiles csv-encrypt-tool.jar \                # my executable jar, path relative to -srcdir
-srcfiles dict \                                # one of my resource directories, path relative to -srcdir
-srcfiles config \                              # another one of my resource directories, path relative to -srcdir
-srcfiles log \                                 # again, one of my resource directories, path relative to -srcdir
-outdir ./dist \                                # where I want the package to be put
-outfile csv-encrypt-tool \                     # the output file name without extension, the final file (or bundle which is a directory actually) will be csv-encrypt-tool.app
-appclass some.package.CsvEncToolApp \          # the class with main method, which is the entry point of the whole app
-name "csv-encrypt-tool" \                      # the name (not very sure what this is for)
-title "csv-encrypt-tool" \                     # the title (not sure what it is either)
-nosign \                                       # not sign the app since this is just an internal tool, if you want to publish it, signing is necessary
-v \                                            # verbose
-BjvmOptions=-Xmx4096m \                        # I need 4GB max heap size
-BmainJar=csv-encrypt-tool.jar \                # the jar file with main class
-Bicon=icon.icns                                # the icon

After the command is executed, there will be some files and directories created in dist, where I want the package be, and one of the directories is bundles. The application is put in there.

Since I just built an internal tool, there is no need to sign and packaged without other production ready options. You can refer to the official manual for help.

Hope this help others who do not know how to package an application on macOS in 2020, just like me.

Programus
  • 338
  • 2
  • 16
0

So none of these options worked for me (maybe because I am running OS X 10.15, maybe because most of these projects are years old, who knows). Installing Catalina made the existing app I had built around the Java app no longer work.

Ultimately, this post helped: Just use Automator to run a script that runs the java command to launch the jar. I wanted to make an app to launch Colossus, a Java version of the old Avalon Hill board game Titan. I wrote a shell script that looks like:java -Xmx256m -jar /my/path/to/the/game/Colossus.jar net.sf.colossus.appmain.Start and then created an automator application whose only action was "Run Shell Script" that launches that script. Works like a charm, no installing Ant, no command line apps requiring you to download java vms, and best of all it uses an Apple tool so will work with newer versions of OS X.

Cheefachi
  • 105
  • 7
-1

You can try this app , it bundles your jar file into Mac app

Edit: it's easy to use , select the Jar file and an Icon. Here you can see the screen shoot.

https://github.com/aprsn/Mac-App-Creator

Alper San
  • 1
  • 1
  • 1
  • 1
    You should, in the interest of full disclosure, indicate that you're the author of the code you're recommending. Also, you might mention how this solution improves upon the many that already exist. – chb Feb 10 '18 at 17:25
  • instead of providing link you should provide summery as an answer along with the link – Nihal Feb 10 '18 at 17:37
  • It’s just easy to use. I think it’s unnecessary to explain. Choose the jar file choose the icon and then give it a name. It just creates an app. If it’s wrong to share your own application , I’m sorry. I can delete the answer. – Alper San Feb 10 '18 at 17:41
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/18784041) – AwkwardCoder Feb 10 '18 at 19:51
  • @AwkwardCoder next time I will be careful – Alper San Feb 10 '18 at 20:47