简体   繁体   中英

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....

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

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.

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.

java包错误

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

蚀

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包

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

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM