Run .jar Files on Mac Big Sur by clicking

I recently switched from Windows to Mac and tried to open an executable .jar file on my Mac by clicking the icon in Finder. Somehow it turned out that mac is not able to execute a .jar file except using the Terminal.

After some research, I got to know that "javapackager" and "Jar Bundler" are ways to make it runnable, but I could not find any functional download for these programs.

So is there any way to make these programs runnable?

Upvotes: 3

Views: 3164

Answers (2)

rcelik
rcelik

Reputation: 56

Actually, I am new to Mac, and I just want to try, and it worked. What I do is, creating a maven project and adding some plugins to create executable jar file. Maven plugins side;

    <build>
    <finalName>myExecutableApp</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/libs
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>libs/</classpathPrefix>
                        <mainClass>
                            com.rcelik.executable.ExecutableMavenJar
                        </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

And some swing code to understand if executable jar file is running.

public class ExecutableMavenJar {
public static void main(String[] args) {
    JFrame f=new JFrame();

    JButton b=new JButton("click");
    b.setBounds(130,100,100, 40);

    f.add(b);

    f.setSize(400,500);
    f.setLayout(null);
    f.setVisible(true);
}

}

After code part, I just package the project with maven. And I could run the executable jar file by double clicking on it. Can you please check this way of creating the executable jar file. By the way, MacOs opens the jar file with JavaLauncher.app which is default.

Upvotes: 0

rzwitserloot
rzwitserloot

Reputation: 102814

The distribution model of shipping a jar file to a user and then expecting them to just know what to do with it is obsolete - oracle has more or less acknowledged that the vast majority of desktop java apps are not now and never were deployed that way: They have stopped supporting the idea of a 'JRE'. This is how it more or less officially (in that oracle's products clearly endorsed this model) used to work:

  • App developer makes a java app, turns it into a bunch of jar files, and ships it as a zip or with an extremely simple installer that just copies them someplace.
  • User of app contracts with Oracle to download a JRE; maintenance of this JRE (because somebody needs to update that thing or at least tell the user there is a security leak in it, if that happens) is an arrangement entirely between user (or possibly OS maintainer) and oracle; app developer has no part in this, and has no control over it either.
  • If there is no JRE available, the user must take care of this, app developer has no real control over this.
  • App developer more or less just prays that their app works well on whatever JRE is used when their jar is double clicked. If it's the wrong version, it's not like you can include a legacy notification or some such, or even specify meaningfully within a jar file which version(s) of java you are compatible with.

This is obsolete, in that oracle has stopped shipping a JRE and has stopped wanting to be 'on the hook' for having a relationship with every end user system on the planet to support those JREs.

Thus, you are labouring under an old deployment mechanism. If you're the owner of this app, update to a new model (such as using jlink and jpackager to make a .app and ship it in a .dmg like normal mac apps, also, this lets you sign it, have a nice icon, etc). If you're not, time to talk to the maintainer of this app and tell them to do that. Until then, here's how you work around this:

  • Go to https://adoptopenjdk.net and download an openjdk.
  • Install it.
  • Put a recurring appointment in your agenda: Every month or so, visit adoptopenjdk.org and check for updates. Uninstall the openjdk you had and install the new one. Failure to do this means your system can be compromised. AdoptOpenJDK isn't really meant for this kind of thing (nothing out there really is, except Oracle's JRE8, but [A] it's 10 years old at this point, and [B] oracle already stopped supporting it, or will soon, at which point that is a security issue as well), hence why you have to put in the effort.
  • Double click the jar.

Upvotes: 3

Related Questions