如何创建一个可以通过单击运行的 Java 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2288440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a Java application which can be run by a click?
提问by Roman
I would like to have a Java application which can be easily started.
我想要一个可以轻松启动的 Java 应用程序。
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).
到目前为止,我已经设法创建了一个 jar 文件,但我还没有看到任何优势。在我通过“java HelloWorldSwing”运行我的应用程序之前,现在我使用“java -jar HelloWorldSwing.jar”(这比之前的命令更复杂,据我所知,使用 jar 文件需要存在 * .mf 文件在同一目录中)。
I would like to have one of the two following situations:
我想有以下两种情况之一:
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)).
Create a pictogram which can be put on a desktop and clicking on which initiates my Java program.
只有一个可以复制到另一个操作系统的文件,并且只需在文件浏览器中单击此文件即可启动项目(此时如果单击我的 jar 文件 Ubuntu 开始提取存档(因为 jar 是一个存档) , 我知道))。
创建一个可以放在桌面上的象形图,然后单击它会启动我的 Java 程序。
回答by Jeeva Subburaj
Here some links that may help u..
这里有一些可能对你有帮助的链接。
JSmooth is a Java Executable Wrapper. It creates native Windows launchers
JSmooth 是一个 Java 可执行文件包装器。它创建本机 Windows 启动器
http://jsmooth.sourceforge.net/
Cross-platform Java executable wrapper
跨平台 Java 可执行包装器
http://launch4j.sourceforge.net/
http://www.captain.at/programming/java/executable-java-application.php
回答by Petteri Hietavirta
回答by Kilian Foth
You can create an executable jar file, as described here:
您可以创建一个可执行的 jar 文件,如下所述:
http://csdl.ics.hawaii.edu/~johnson/613f99/modules/04/jar-files.html
http://csdl.ics.hawaii.edu/~johnson/613f99/modules/04/jar-files.html
Then users can click on it in the file browser and have the main class start, provided the OS and the user's security settings allow it.
然后用户可以在文件浏览器中单击它并启动主类,前提是操作系统和用户的安全设置允许。
回答by ewernli
Making a jar with no dependenciesexecutable 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可执行文件相对容易。您基本上只需要MANIFEST
在主类中指定即可使用。然后可以用 启动java -jar ExecutableJar.jar
,但大多数操作系统在这种情况下支持双击。
Making a jar that depends on other jarexecutable 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.
制作一个依赖于其他 jar可执行文件的jar更加棘手。您可以在 中指定类路径MANIFEST
,但这仍然意味着您需要复制多个 jar 来分发您的应用程序。另一种方法是在主 jar 中“打包”依赖的 jar。
You can do all this with mavenand the maven-assembly-plugin. Use the jar-with-dependencies
preconfigured descriptor.
您可以使用maven和maven-assembly-plugin来完成所有这些。使用jar-with-dependencies
预配置的描述符。
<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.
编辑:一个完整的pom.xml
可以在这个问题中找到。
回答by GuruKulki
You have to create an executable jar, with mentioning your main class(ie a public class with main() method in it) in the jar's manifest file as the Main-Class. So when prople double click it or type: jar "your jar name" in command console then they will get it executed.
您必须创建一个可执行 jar,并在 jar 的清单文件中将您的主类(即带有 main() 方法的公共类)作为 Main-Class 提及。因此,当 prople 双击它或在命令控制台中键入:jar "your jar name" 时,他们将执行它。
回答by Valentin Rocher
回答by Dennis C
It is called WebStart,
它被称为WebStart,
However, it does not perfectly fit your description. It does not provide a self-packed solution which should exists.
但是,它并不完全符合您的描述。它不提供应该存在的自打包解决方案。
PS. I personally believe executable jar is a joke, you can't control the vmargs, can't have a good icon, and you can't even know it is executable until it is being unpack and try to be execute.
附注。我个人认为可执行 jar 是个笑话,你无法控制 vmargs,不能有一个好的图标,甚至在它被解压并尝试执行之前你甚至不知道它是可执行的。
回答by Chris J
I actually really like how maven creates them: http://maven.apache.org/shared/maven-archiver/examples/classpath.html#Make
我真的很喜欢 maven 如何创建它们:http: //maven.apache.org/shared/maven-archiver/examples/classpath.html#Make