简体   繁体   English

如何创建我的 java 项目的 .app 文件以在 mac os 上运行

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

I am new to mac.我是 mac 新手。 I have a java project.我有一个java项目。 I created a exe of that project using launch4j for window.我使用launch4j为窗口创建了该项目的exe。 Now I need to create a application for mac.现在我需要为 mac 创建一个应用程序。 I java project contains 5 java classes, also referred to some external jars.我的java项目包含5个java类,也引用了一些外部jar。 I found this site http://www.centerkey.com/mac/java/ .我找到了这个网站http://www.centerkey.com/mac/java/ But I struck when I tried to create the executable jar file.但是当我尝试创建可执行 jar 文件时,我感到震惊。 While working on window I used the following commands to create the class files and jar file.在窗口上工作时,我使用以下命令创建类文件和 jar 文件。

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 文件

jar cvf one.jar one.class

I used the same command in mac terminal.我在 mac 终端中使用了相同的命令。 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. AFAIK Eclipse 可以为 Java 项目创建 Mac 应用程序包,但我没有使用它,也不能说它是如何工作的。

Try Export -> Other -> Mac OS X Application bundle尝试导出 -> 其他 -> Mac OS X 应用程序包

Update on the above: The "Export -> Other -> Mac OS X Application bundle" does not work for me on current Eclipse and Java stuff.更新上述内容:“导出 -> 其他 -> Mac OS X 应用程序包”在当前的 Eclipse 和 Java 内容上对我不起作用。 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.试图绕过这个绊脚石,我发现了以下内容: https : //centerkey.com/mac/java/我尝试了他们的教程示例,并且奏效了。

First thing first, DO NOT USE javapackager首先,不要使用 javapackager

javapackager is the packaging and signing tool released with JDK 8; javapackager 是JDK 8 发布的打包签名工具; When JDK 11 deleted javaFX, javapackager is also deleted as a part of it.当 JDK 11 删除 javaFX 时,javapackager 也作为其中的一部分被删除。

That's why you may encounter below issue when you try to use javapackager:这就是当您尝试使用 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我如何设法打包自包含的 Java 应用程序

  1. Use Eclipse to generate runnable JAR file使用 Eclipse 生成可运行的 JAR 文件

蚀

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获取生成的文件

在此处输入图片说明

In the current terminal path ~/ , HelloSwing-1.0.dmg (51MB) is generated, that is the install file.在当前终端路径 ~/ 中,生成了 HelloSwing-1.0.dmg (51MB),即安装文件。

Under ~/Downloads/temp, HelloSwing.app is generated (125MB), double click to launch the App.在~/Downloads/temp下生成HelloSwing.app (125MB),双击启动App。

This is just a Hello World project of Java Swing, however, the application image size is a bit daunting.这只是 Java Swing 的一个 Hello World 项目,但是,应用程序图像大小有点令人生畏。

Anyway, happy coding!无论如何,快乐编码!

Reference: jpackage command doc参考: jpackage 命令文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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