Skip to content
Advertisement

Java executable jar cannot be executed as ./program.jar on OS X

Using Linux, I run some programs I’ve written in Java by compressing them in a jar with a manifest file and then placing them in a personal directory that has been added to my $PATH as follows: program.jar file2 file2. It works well in any directory I point my console to, I can even do program.jar file1 | less or program.jar file1 > output.txt.They work as if they were bin console programs.

However on OS X I cannot replicate the same behavior, I am forced to run them with java -jar program.jar otherwise it would say -bash program.jar: cannot execute binary file

Is there a way to make a jar behave as they do in Linux on OS X?

EDITED:

ANSWER thanks to Jean-Baptiste Yunès:


From the command line

If you build a new Java Swing application using one of the Xcode Organizer’s templates, Xcode automatically generates an application bundle complete with a default Info.plist file. You can fine-tune the Info.plist file directly in Xcode or with Property List Editor. For more information on using Xcode for Java development, see Xcode Help (available from the Help menu in Xcode).

If you want to turn your existing Java application into an OS X Java application, use the Jar Bundler application available in /Developer/Applications/Utilities. It allows you to take existing .class or .jar files and wrap them as application bundles. Information about Jar Bundler, including a tutorial, is provided in Jar Bundler User Guide.

To build a valid application bundle from the command-line, for example, in a shell script or an Ant file, you need to follow these steps:

Set up the correct directory hierarchy. The top level directory should be named with the name of your application with the suffix .app.

There should be a Contents directory at the root of the application bundle. It should contain a MacOS directory and a Resources directory. A Java directory should be inside of the Resources directory.

The directory layout should look like this:

YourApplicationName.app/
    Contents/
        MacOS/
        Resources/
            Java/

Copy the JavaApplicationStub file from /System/Library/Frameworks/JavaVM.framework/Versions/Current/Resources/MacOS/ into the MacOS directory of your application bundle.

Make an Info.plist file in the Contents directory of your application bundle. You can start with an example from an existing Java application (such as Jar Bundler) and modify it or generate a completely new one from scratch. Note that the application bundle does not launch unless you have set the correct attributes in this property list, especially the MainClass key.

Make a PkgInfo file in the Contents directory. It should be a plain text file. If you have not registered a creator code with ADC, the contents should be APPL????. If you have registered a creator code replace the ???? with your creator code.

Put your application’s icon file into the Contents/Resources/ directory. Use Icon Composer in Developer/Applications/Utilities for help creating your icon file.

Copy your Java .jar or .class files into Contents/Resources/Java/.

Set the bundle bit Finder attribute with SetFile, found in /Developer/Tools/. For example, /Developer/Tools/SetFile -a B YourApplicationName.app. For more information on SetFile, see the man page.

After these steps, you should have a double-clickable application bundle that contains your Java application.

Advertisement

Answer

OSX Apps need to be bundled, so read Oracle Java OSX Distribution and Apple Java Bundles. After that you will be able to launch them via command-line (just ask to run the stub by hand).

Advertisement