Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Native OSX Executable for Java from Windows

I have a program written in java that I'd like to provide native-style wrappers for. My target platforms are OSX, Windows, and Linux.

I have Windows and Linux working "good enough" right now. It'd be nice to provide a windows installer, a linux rpm, and a linux .deb, but for now I'm relatively satisfied with the package I provide to the user on those two platforms. I think it is relatively intuitive, feels native, and is easy to use.

For Windows

  • I use launch4j to create a native executable.
  • I package the native executable, jars, stripped JRE, and resource files in .zip
  • The user downloads the zip, extracts the folder inside, and double clicks the executable.

While this method doesn't have an installer, I feel it's "native-enough".

For Linux

  • I have a simple C++ program serving as a native 32-bit executable, which launches java targeting my jar file.
  • I package the native executable, jars, stripped JRE, and resource files in .tar.gz
  • The user downloads the .tar.gz, extracts the folder inside, and double clicks the executable (or calls it from the console).

While I think it would be nice to distribute via .rpms and .debs, and to provide native icon support for at least KDE and gnome, I'm also happy with this result for the time being.

Here is the native executable code, for anyone who is interested.

/*Compile this on a linux machine to create a local nix executable
    g++ -m32 -o executable-name this-file-name.cpp
    -m32 forces 32 bit mode, which should help compatibility
*/

#include <stdio.h>
#include <cstdlib>

int main() {

    int result = system( "java -jar TARGET_JAR.jar 2> /dev/null > /dev/null " );

    if ( result != 0 ) {
        printf ( "PROGRAM_NAME requires Java, but Java isn't in your path. Please make sure Java is installed and 'java' is visible in your path. Once you've done that, please run this executable to run PROGRAM_NAME!\n" );
    }
}

I intend to modify this for the upcoming release to also use an embedded jre, but that is a trivial change.

For OSX

I don't have a working system yet. Here is what I'd like:

  • User downloads a .dmg file, which contains an .app.
  • I'd like for the .app to:
    • Have an embedded JRE
    • Be double clickable
    • Build can be automated with ANT.

My previous attempts at creating this app failed miserably. I tried:

  • Appbundler: I could not get the examples to work. I believe the source of the problem was working in a windows environment, but perhaps I was just doing things wrong.

  • Rolling my own .app: This failed, as you can see in the thread.

  • javapackager (included with java 8): I similarly could not get this to work. As it's a new tool, there is a sparsity of examples in the wild, and the tool seems immature and focused on webstart; the windows installer I got when trying to create the native windows package was primitive and I could not get it to include other non-jar resources.

  • webstart: I don't want .jnlps. I can't have icons or embedded jres.

I feel like there should be an easy way to roll my own .app. As far as I can tell, apps are just directories with special structures and a Info.plist.

However, I'm open to any suggestions that work. In the end, as long as I get a package that feels native on OSX and can be automated with ANT, I'll be very happy.

Thank you!

like image 396
JoshuaD Avatar asked Apr 11 '26 02:04

JoshuaD


1 Answers

You will need a Mac computer with Xcode installed in order to do this.

like image 73
Danny Bravo Avatar answered Apr 13 '26 15:04

Danny Bravo