如何创建一个点击即可运行的Java应用程序? [英] How to create a Java application which can be run by a click?

查看:32
本文介绍了如何创建一个点击即可运行的Java应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个易于启动的 Java 应用程序.

到目前为止,我已经设法创建了一个 jar 文件,但我还没有看到任何优势.在我通过java HelloWorldSwing"运行我的应用程序之前,我现在使用java -jar HelloWorldSwing.jar"(这比之前的命令更复杂,据我所知,使用 jar 文件需要存在 *.mf 文件在同一目录).

我想有以下两种情况之一:

  1. 只有一个文件,可以复制到另一个操作系统,只需在文件浏览器中单击此文件即可启动项目(此时如果单击我的 jar 文件 Ubuntu 开始提取存档(因为 jar 是一个存档,我知道)).

  2. 创建一个象形图,它可以放在桌面上并点击它启动我的 Java 程序.

解决方案

制作一个 没有依赖关系的 jar 可执行文件相对容易.您基本上只需要在 MANIFEST 中指定要使用的主类.然后可以使用 java -jar ExecutableJar.jar 启动它,但大多数操作系统在这种情况下都支持双击.

制作依赖于其他 jar 的 jar 可执行文件更加棘手.您可以在 MANIFEST 中指定类路径,但这仍然意味着您需要复制多个 jar 来分发您的应用程序.另一种方法是在主 jar 中打包"依赖的 jar.

您可以使用 ma​​venmaven-assembly-plugin.使用 jar-with-dependencies 预配置的 描述符.

<预><代码><配置><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs></配置>

主类可以指定为:

<清单><mainClass>org.company.MyClass</mainClass></清单></归档>

编辑:可以在 这个问题.

I would like to have a Java application which can be easily started.

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:

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

  2. Create a pictogram which can be put on a desktop and clicking on which initiates my Java program.

解决方案

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.

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.

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>

EDIT: A complete pom.xml can be found in this question.

这篇关于如何创建一个点击即可运行的Java应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆