如何创建可以通过单击运行的Java应用程序? [英] How to create a Java application which can be run by a click?
问题描述
我想要一个可以轻松启动的Java应用程序。
I would like to have a Java application which can be easily started.
到目前为止,我已经设法创建了一个jar文件,但我还没有看到任何优势。在我通过java HelloWorldSwing运行我的应用程序之前,现在我使用java -jar HelloWorldSwing.jar(这比上一个命令更复杂,据我所知,jar文件的使用需要存在* .mf文件在同一目录中。)
So far I have managed to create a jar file but I do not see any advantages yet. Before I run my application by "java HelloWorldSwing" and now I use "java -jar HelloWorldSwing.jar" (which is even more complicated than the previous command and, as far as I understood, the usage of a jar file requires presence of *.mf file in the same directory).
我想要以下两种情况之一:
I would like to have one of the two following situations:
-
只需一个文件即可复制到另一个操作系统,只需在文件浏览器中单击此文件即可启动项目(此时如果单击我的jar文件,则Ubuntu开始提取存档(因为jar是一个存档,我知道)。)
Just one single file which can be copied to another operation system and that the project can be started just by click on this file in a file browser (at the moment if click on my jar file Ubuntu starts to extract archive (because jar is an archive, I know)).
创建一个象形图,可以放在桌面上点击哪个启动我的Java程序。
Create a pictogram which can be put on a desktop and clicking on which initiates my Java program.
推荐答案
制作一个没有依赖关系的 jar 可执行文件相对简单。您基本上只需要在 MANIFEST
中指定要使用的主类。然后可以使用 java -jar ExecutableJar.jar
启动它,但在这种情况下大多数操作系统支持双击。
Making a jar with no dependencies executable is relatively easy. You basically just need to specify in the MANIFEST
the main class to use. It can then be started with java -jar ExecutableJar.jar
, but most OS support double-click in this case.
制作一个依赖于其他jar 可执行文件的 jar更加棘手。您可以在 MANIFEST
中指定类路径,但这仍然意味着您需要复制多个jar来分发您的应用。另一种方法是在主罐中包装悬浮罐。
Making a jar that depends on other jar executable is more tricky. You can specify the class path in the MANIFEST
, but it still means you need to copy more than one jar to distribute your app. Another way is to "pack" the depending jar in the main jar.
您可以使用 maven 和 maven-assembly-plugin 。使用预先配置的 jar-with-dependencies
描述符。
You can do all this with maven and the maven-assembly-plugin. Use the jar-with-dependencies
preconfigured descriptor.
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
可以使用以下内容指定主类:
The main class can be specified with something like:
<archive>
<manifest>
<mainClass>org.company.MyClass</mainClass>
</manifest>
</archive>
编辑:完整的 pom.xml
可在中找到这个问题。
这篇关于如何创建可以通过单击运行的Java应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!