简体   繁体   English

如何使用Eclipse在JAVA中创建可执行文件(双击启动程序的文件,如.exe)?

[英]How to create an executable (a file that starts the program on double-click like an .exe) in JAVA with Eclipse?

I've created a program in java and now I want to create an executable from it. 我用java创建了一个程序,现在我想从中创建一个可执行文件。 I'm new to java, I don't know if it should be a .exe . 我是java新手,我不知道它应该是.exe I'we exported my project to a .jar file, but when I double-click it it opens "open with" window. 我将我的项目导出到.jar文件,但是当我双击它时,它会打开“打开方式”窗口。

I want to export my project to a file that runs my program on double-click. 我想将项目导出到双击运行程序的文件中。

Is there any solution? 有什么解决方案吗?

Export --> Java --> Runnable Jar file --> Specify the class with static main method to run. 导出 - > Java - > Runnable Jar文件 - >使用静态main方法指定要运行的类。

Double click on the Jar file to run.. 双击Jar文件运行..

Thanks... 谢谢...

Java compiliation creates byte code for the JVM, so a native, binary executable is not created during compiliation (like C or C++ programs). Java compiliation为JVM创建字节代码,因此在编译期间不会创建本机二进制可执行文件(如C或C ++程序)。 This is a feature of Java. 这是Java的一个特性。

To deploy an application to multiple systems they must have the JRE. 要将应用程序部署到多个系统,它们必须具有JRE。 These .jar files can be launched from the command line (see this: http://download.oracle.com/javase/tutorial/deployment/jar/run.html ) 可以从命令行启动这些.jar文件(请参阅: http//download.oracle.com/javase/tutorial/deployment/jar/run.html

Some vendors get around this with batch files that launch the JRE to run their application's JAR (and then put these in the start menu, desktop, etc with a fancy icon). 一些供应商利用批处理文件来解决这个问题,这些批处理文件启动JRE以运行其应用程序的JAR(然后将它们放在开始菜单,桌面等中,并带有花哨的图标)。

If you want people to install your app (especially from a web page or over a network) you probably want a Java Web Start package (see this for crating one in Eclipse: http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fjava_web_start.htm ) 如果您希望人们安装您的应用程序(特别是从网页或网络上),您可能需要一个Java Web Start软件包(请参阅此文章,以便在Eclipse中创建一个: http//help.eclipse.org/indigo/index。 jsp?topic =%2Forg.eclipse.platform.doc.isv%2Fguide%2Fjava_web_start.htm

If you just want it to be runnable on your computer, you can use the open with dialog to open it with javaw.exe in JDK_DIRECTORY\\bin. 如果您只是希望它可以在您的计算机上运行,​​您可以使用打开对话框在JDK_DIRECTORY \\ bin中使用javaw.exe打开它。 Alternatively, if you really wanted to make it an EXE, you could look at some of the answers here . 或者,如果你真的想让它成为一个EXE,你可以在这里看一些答案。

Either do as in the link mentioned by @dacwe or I would suggest to depending on operating system set a permanent connection between java and jars, then it will always happen. 要么像在@dacwe提到的链接中那样做,要么我建议根据操作系统设置java和jar之间的永久连接,那么它总会发生。 On Windows you do it by right clicking on any jar then open with and find your javaw.exe in bin folder of your jre installation. 在Windows上,您可以通过右键单击任何jar然后打开并在jre安装的bin文件夹中找到您的javaw.exe来执行此操作。

I think you are looking for a simpler approach than Java Web Start. 我认为您正在寻找比Java Web Start更简单的方法。

I assume that you are a Windows User, since you want a .exe file. 我假设您是Windows用户,因为您需要.exe文件。

So once you have the exported MyProgram.jar file, you only need to create a .bat file that contains a sole line: 因此,一旦获得导出的MyProgram.jar文件,您只需要创建一个包含唯一行的.bat文件:

java -jar MyProgram.jar java -jar MyProgram.jar

and place this execute.bat file in the same folder as your MyProgram.jar 并将此execute.bat文件放在与MyProgram.jar相同的文件夹中

This approach works for Linux too, only you need to place it inside a similar file and execute the same command. 这种方法也适用于Linux,只需要将它放在类似的文件中并执行相同的命令。

Read here http://javabeanz.wordpress.com/2009/01/29/running-an-executable-jar-from-command-line/ for more explanations. 请阅读http://javabeanz.wordpress.com/2009/01/29/running-an-executable-jar-from-command-line/以获取更多解释。

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

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