10

I created a .jar that requires Java 7. I have Java 7 (JDK and JRE) and I can double-click to run the .jar. However, I want to package this into an application.

What I tried: Using Apple's Jar Bundler tool, I successfully created an application; however, when I run it, it quickly appears and disappears in the dock. When I run the internal file JavaApplicationStub from Terminal, I get:

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/lcmmun/kiosk/gui/Kiosk : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at apple.launcher.LaunchRunner.loadMainMethod(LaunchRunner.java:56)
    at apple.launcher.LaunchRunner.run(LaunchRunner.java:112)
    at apple.launcher.LaunchRunner.callMain(LaunchRunner.java:51)
    at apple.launcher.JavaApplicationLauncher.launch(JavaApplicationLauncher.java:52)

which, according to "unsupportedclassversionerror unsupported major.minor version 51.0 unable to load class" means that I don't have Java 7 installed. But I do.

So my hypothesis is that JavaApplicationStub is Java 7-incompatible.

I've also heard of an ANT task to accomplish the same task, but it seemed too complicated, and I don't know how to use ANT. If this really is the solution, though, I'd be happy to learn.

EDIT: I have now also tried Eclipse's Export > Other > Mac OS Application Bundle. This fails as well.

I'm running 10.8 Mountain Lion, 64-bit.

Community
  • 1
  • 1
wchargin
  • 15,589
  • 12
  • 71
  • 110
  • *"I created a .jar that requires Java 7"* What Java 7 functionality does it use? *"I want to package this into an application."* Why not deploy it using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info)? – Andrew Thompson Aug 24 '12 at 05:52
  • @AndrewThompson: Java Web Start - interesting. Would I have to rewrite a lot of my code? I have file loading/saving and `Desktop.open`, as well as oodles of Swing code. – wchargin Aug 25 '12 at 03:35
  • To use the usual APIs for file I/O or for use of `Desktop`, the code would need to be digitally signed by you and trusted by the end user (click 'OK' when prompted). That would require 0 changes to the code. There are APIs available only to apps. launched by JWS that provide a slightly different and more limited form of [file I/O](http://pscode.org/jws/api.html#fs) and [desktop](http://pscode.org/jws/api.html#bs) to a sand-boxed app., but to use those APIs *would* require changes to the code. – Andrew Thompson Aug 25 '12 at 04:02
  • Sounds good. I assume the Preferences API (`java.util.prefs`) is supported? – wchargin Aug 25 '12 at 19:40
  • All of J2SE classes are supported for a trusted app. launched using JWS. – Andrew Thompson Aug 25 '12 at 23:19
  • If you want to assume anybody can use your app without installing stuff first, Java 7 is probably a bad choice. – jahroy Oct 14 '12 at 03:04

1 Answers1

17

Issue

You are definitely right with your assumption that Apples JavaApplicationStub located here:

  • System/Library/Frameworks/JavaVM.framework/Resources/MacOS/JavaApplicationStub

is only compatible with Apples own Java Packages build for Mac OS X.

Reason

Apple is discontinuing their own Java System Packages and only supports Java 6 in their fade out process. Oracle took now over and provides Java 7 for Mac OS X from 10.7.3 on. Apple is even discontinuing developer tools like Jar Bundler.app without any notice, as you can see in this post:

Some people even tried desperately to manually increase the JVMVersion property value in Info.plist, read by Apples JavaApplicationStub, from a documented literal 1.6 or 1.6+ to an undocumented literal like 1.7. This won't work either and you will end up with a dialog like this, even if you have installed Oracles Java 7 Package.

UserNotificationCenter

So it is likely you will find other inconstancies between the retired Apple Java 6 world and the future Oracle Java 7 world.

Solution

To build an application packages based on Oracles Java 7 you need to use Oracles AppBundler Ant Task containing Oracles JavaAppLauncher. This one now only supports Oracles Java 7 for Mac OS X and isn't backwards compatible with Apples own Java System Packages.

The good news now is, you can inline Oracles Java 7 JRE into your application package. It will be contained within the directory

  • Contents/PlugIns

in the application package, for example

  • Contents/PlugIns/jdk1.7.0_17.jdk

This means your application package is totally self contained and ready for App Store deployment.

But you don't have to do that. You could also rely on the installed Oracle Java 7 Package.

For a more detailed answer you should also checkout:

Community
  • 1
  • 1
Uwe Günther
  • 2,971
  • 2
  • 21
  • 26
  • So there's no way to just get the application to run `java -jar` on my jarfile? Is there any way I could embed a simple bash script as an executable for my application? Something like [this](http://stackoverflow.com/a/7335524/732016) would get the job done fine; then I wouldn't have to worry about the JDK provider, etc. – wchargin Dec 26 '13 at 16:44
  • 1
    Of course there is, but the question here is about how to startup Java applications the Apple way, also known as OSX App-Bundles. So instead using the 'java' binary to startup a Java main class, you use a stub which is integrated in an OSX app bundle. – Uwe Günther Dec 26 '13 at 16:57
  • 1
    So should the question "What is the easiest, most-portable way to package a Java 7 jar file into a native Mac application" be a separate question? – wchargin Dec 26 '13 at 20:11
  • 2
    Hi Guys, thanks for your question and answer, as the answer is back to 2013, have you came across any better solution to bundle java applications for mac? – Jack Jan 07 '15 at 03:55
  • @Jack is there anything about this solution that you think is lacking? – Mark Lalor Aug 24 '15 at 17:50
  • @MarkLalor something is wrong with it, I tried to bundle my application by following this solution to no avail. I do not really remember what was the issue but as I can say from my comment some of the applications mentioned are not compatible or do not exist any more. Thats why I asked if there is any update for it. – Jack Aug 25 '15 at 03:28
  • @Jack I finally got around to implementing this in my own project. It seems to be working fine. Perhaps you should give it another shot and post a question on StackOverflow if you run into a problem. You could also try [packr](https://github.com/libgdx/packr) which I've used. I have yet to find out what makes this different from jar bundler besides the fact that it's not an ant task but a command line tool. – Mark Lalor Sep 01 '15 at 22:01
  • @MarkLalor alright thanks, I will post a new question then. – Jack Sep 09 '15 at 06:47