Manikandan
Manikandan

Reputation: 1519

How to create a .app file of my java project to run on mac os

I am new to mac. I have a java project. I created a exe of that project using launch4j for window. Now I need to create a application for mac. I java project contains 5 java classes, also referred to some external jars. I found this site http://www.centerkey.com/mac/java/. But I struck when I tried to create the executable jar file. While working on window I used the following commands to create the class files and jar file.

To create class files..

javac one.java two.java -cp mail.jar;sqlite.jar Mainclass.java

To create the jar files for the classes created from the above command

jar cvf one.jar one.class

I used the same command in mac terminal. But the first command to create the class files doesn't work. Any suggestion....

Upvotes: 3

Views: 5038

Answers (3)

Chandler
Chandler

Reputation: 3295

First thing first, DO NOT USE javapackager

javapackager is the packaging and signing tool released with JDK 8; When JDK 11 deleted javaFX, javapackager is also deleted as a part of it.

That's why you may encounter below issue when you try to use javapackager:

The operation couldn’t be completed. Unable to locate a Java Runtime that supports javapackager.

javapackage error

I specifically mention it here because there are so many outdated info throughout the internet, cost me so much time going round in circles.

How I managed to package self-contained Java Application

  1. Use Eclipse to generate runnable JAR file

eclipse

a. Right click your project -> Export.
b. Select Java -> Runnable JAR file.
c. Next.
d. Specify Export destination, e.g. ~/Downloads/jar/HelloSwing.jar .
e. "Library handling" select "Extract required libraries into generated JAR".
f. Finish.
  1. Use jpackage to package jpackage

Input below command in the termnial:

jpackage --type pkg \
 --temp ~/Downloads/temp \
 --name HelloSwing \
 --input ~/Downloads/jar \
 --main-jar HelloSwing.jar \
 --main-class com.cheng.rostergenerator.ui.Main
  1. Get generated files

enter image description here

In the current terminal path ~/ , HelloSwing-1.0.dmg (51MB) is generated, that is the install file.

Under ~/Downloads/temp, HelloSwing.app is generated (125MB), double click to launch the App.

This is just a Hello World project of Java Swing, however, the application image size is a bit daunting.

Anyway, happy coding!

Reference: jpackage command doc

Upvotes: 1

user9695085
user9695085

Reputation: 11

Update on the above: The "Export -> Other -> Mac OS X Application bundle" does not work for me on current Eclipse and Java stuff. Trying to get around this stumbling block I found the following: https://centerkey.com/mac/java/ I tried their sample for the tutorial, and it worked.

Upvotes: 0

Nikolay Kasyanov
Nikolay Kasyanov

Reputation: 947

AFAIK Eclipse can create Mac app bundles for Java projects, though i'm not used it and can't say how it works.

Try Export -> Other -> Mac OS X Application bundle

Upvotes: 2

Related Questions