将java程序编译成可执行文件[duplicate]

时间:2020-12-10 13:16:35

Possible Duplicate:
How do I create an .exe for a Java program?

可能重复:如何为Java程序创建.exe?

I've just made a simple program with eclipse and I want to compile it into an executable, but simply can't seem to find out how to do it.Please help.

我刚刚用eclipse创建了一个简单的程序,我想将它编译成可执行文件,但似乎无法找到如何做到这一点。请帮忙。

7 个解决方案

#1


124  

You can convert .jar file to .exe on these ways:
将java程序编译成可执行文件[duplicate]
1- JSmooth .exe wrapper:
JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself. When no VM is available, the wrapper can automatically download and install a suitable JVM, or simply display a message or redirect the user to a web site.

您可以通过以下方式将.jar文件转换为.exe:1- JSmooth .exe包装器:JSmooth是一个Java可执行包装器。它为您的Java应用程序创建本机Windows启动程序(标准.exe)。它使Java部署更加顺畅和用户友好,因为它能够自己找到任何已安装的Java VM。当没有可用的VM时,包装器可以自动下载并安装合适的JVM,或者只是显示消息或将用户重定向到网站。

JSmooth provides a variety of wrappers for your java application, each of them having their own behaviour: Choose your flavour!

JSmooth为您的Java应用程序提供了各种包装器,每个包装器都有自己的行为:选择你的味道!

Download: http://jsmooth.sourceforge.net/

2- JarToExe 1.8
Jar2Exe is a tool to convert jar files into exe files.Following are the main features as describe in their website:

2- JarToExe 1.8 Jar2Exe是一个将jar文件转换为exe文件的工具。以下是其网站中描述的主要功能:

  • Can generate “Console”, “Windows GUI”, “Windows Service” three types of exe files.
  • 可以生成“Console”,“Windows GUI”,“Windows Service”三种类型的exe文件。

  • Generated exe files can add program icons and version information.
  • 生成的exe文件可以添加程序图标和版本信息。

  • Generated exe files can encrypt and protect java programs, no temporary files will be generated when program runs.
  • 生成的exe文件可以加密和保护java程序,程序运行时不会生成临时文件。

  • Generated exe files provide system tray icon support.
  • 生成的exe文件提供系统托盘图标支持。

  • Generated exe files provide record system event log support.
  • 生成的exe文件提供记录系统事件日志支持。

  • Generated windows service exe files are able to install/uninstall itself, and support service pause/continue.
  • 生成的Windows服务exe文件能够自行安装/卸载,并支持服务暂停/继续。

  • New release of x64 version, can create 64 bits executives. (May 18, 2008)
  • 新版x64版本,可以创建64位高管。 (2008年5月18日)

  • Both wizard mode and command line mode supported. (May 18, 2008)
  • 支持向导模式和命令行模式。 (2008年5月18日)

Download: http://www.brothersoft.com/jartoexe-75019.html

3- Executor
Package your Java application as a jar, and Executor will turn the jar into a Windows exe file, indistinguishable from a native application. Simply double-clicking the exe file will invoke the Java Runtime Environment and launch your application.

3-执行程序将Java应用程序打包为jar,Executor将jar转换为Windows exe文件,与本机应用程序无法区分。只需双击exe文件即可调用Java Runtime Environment并启动您的应用程序。

Download: http://mpowers.net/executor/

EDIT: The above link is broken, but here is the page (with working download) from the Internet Archive. http://web.archive.org/web/20090316092154/http://mpowers.net/executor/

编辑:以上链接已断开,但这是来自Internet Archive的页面(有工作下载)。 http://web.archive.org/web/20090316092154/http://mpowers.net/executor/

4- Advanced Installer
Advanced Installer lets you create Windows MSI installs in minutes. This also has Windows Vista support and also helps to create MSI packages in other languages.
Download: http://www.advancedinstaller.com/Let me know other tools that you have used to convert JAR to EXE.

4- Advanced Installer Advanced Installer允许您在几分钟内创建Windows MSI安装。这也支持Windows Vista,也有助于使用其他语言创建MSI包。下载:http://www.advancedinstaller.com/告诉我您用于将JAR转换为EXE的其他工具。

#2


31  

I would use GCJ (GNU Compiler for Java) in your situation. It's an AOT (ahead of time) compiler for Java, much like GCC is for C. Instead of interpreting code, or generating intermediate java code to be run at a later time by the Java VM, it generates machine code.

我会在你的情况下使用GCJ(GNU Compiler for Java)。它是用于Java的AOT(提前)编译器,就像GCC用于C而不是解释代码,或者生成中间Java代码以便稍后由Java VM运行,它生成机器代码。

GCJ is available on almost any Linux system through its respective package manager (if available). After installation, the GCJ compiler should be added to the path so that it can be invoked through the terminal. If you're using Windows, you can download and install GCJ through Cygwin or MinGW.

GCJ几乎可以通过其各自的包管理器(如果可用)在任何Linux系统上使用。安装后,应将GCJ编译器添加到路径中,以便可以通过终端调用它。如果您使用的是Windows,则可以通过Cygwin或MinGW下载并安装GCJ。

I would strongly recommend, however, that you rewrite your source for another language that is meant to be compiled, such as C++. Java is meant to be a portable, interpreted language. Compiling it to machine code is completely against what the language was developed for.

但是,我强烈建议您重写另一种要编译的语言的源代码,例如C ++。 Java旨在成为一种可移植的解释语言。将其编译为机器代码完全违背了为其开发的语言。

#3


17  

I use launch4j

我用的是launch4j

ANT Command:

<target name="jar" depends="compile, buildDLLs, copy">    <jar basedir="${java.bin.dir}" destfile="${build.dir}/Project.jar" manifest="META-INF/MANIFEST.MF" /></target><target name="exe" depends="jar">    <exec executable="cmd" dir="${launch4j.home}">        <arg line="/c launch4jc.exe ${basedir}/${launch4j.dir}/L4J_ProjectConfig.xml" />    </exec></target>

#4


3  

I usually use a bat script for that. Here's what I typically use:

我通常使用蝙蝠脚本。这是我通常使用的:

@echo offset d=%~dp0java -Xmx400m -cp "%d%myapp.jar;%d%libs/mylib.jar" my.main.Class %*

The %~dp0 extract the directory where the .bat is located. This allows the bat to find the locations of the jars without requiring any special environment variables nor the setting of the PATH variable.

%~dp0提取.bat所在的目录。这允许蝙蝠找到罐子的位置,而不需要任何特殊的环境变量,也不需要设置PATH变量。

EDIT: Added quotes to the classpath. Otherwise, as Joey said, "fun things can happen with spaces"

编辑:在类路径中添加引号。否则,正如乔伊所说,“空间可能会发生有趣的事情”

#5


2  

We have found Jsmooth to be well-working and easily scriptable with ant under Linux. You may want to use one-jar (also easily scriptable with ant under Linux) to collect a multifile application in a single jar first.

我们发现Jsmooth在Linux下运行良好且易于使用ant编写脚本。您可能希望首先在单个jar中使用one-jar(也可以在Linux下轻松编写脚本)来收集多文件应用程序。

We primarily needed the easy deployment of the EXE combined with the "hey, you need Java version X, go here to download" facilities.

我们主要需要轻松部署EXE并结合“嘿,你需要Java版本X,去这里下载”设施。

(but what you most likely need is the "Runnable jar" / "Executable jar" facility in standard Java).

(但你最需要的是标准Java中的“Runnable jar”/“Executable jar”工具)。

#6


1  

The thing you can do is create a .bat that will execute the .jar file created, checking if there is a JRE present.

您可以做的是创建一个.bat,它将执行创建的.jar文件,检查是否存在JRE。

From Mitch useful link (source)

来自Mitch的有用链接(来源)

java -classpath myprogram.jar de.vogella.eclipse.ide.first.MyFirstClass

This can be used into your batch...

这可以用于您的批次...

#7


0  

There is a small handful of programs that do that... TowerJ is one that comes to mind (I'll let you Google for it) but it costs significant money.

有一小部分程序可以做到这一点... TowerJ是一个浮现在脑海中的想法(我会让你谷歌),但它需要花费很多钱。

The most useful reference for this topic I found is at: http://mindprod.com/jgloss/nativecompiler.html

我找到的这个主题最有用的参考是:http://mindprod.com/jgloss/nativecompiler.html

it mentions a few other products, and alternatives to achieve the same purpose.

它提到了一些其他产品,以及实现相同目的的替代品。

#1


124  

You can convert .jar file to .exe on these ways:
将java程序编译成可执行文件[duplicate]
1- JSmooth .exe wrapper:
JSmooth is a Java Executable Wrapper. It creates native Windows launchers (standard .exe) for your java applications. It makes java deployment much smoother and user-friendly, as it is able to find any installed Java VM by itself. When no VM is available, the wrapper can automatically download and install a suitable JVM, or simply display a message or redirect the user to a web site.

您可以通过以下方式将.jar文件转换为.exe:1- JSmooth .exe包装器:JSmooth是一个Java可执行包装器。它为您的Java应用程序创建本机Windows启动程序(标准.exe)。它使Java部署更加顺畅和用户友好,因为它能够自己找到任何已安装的Java VM。当没有可用的VM时,包装器可以自动下载并安装合适的JVM,或者只是显示消息或将用户重定向到网站。

JSmooth provides a variety of wrappers for your java application, each of them having their own behaviour: Choose your flavour!

JSmooth为您的Java应用程序提供了各种包装器,每个包装器都有自己的行为:选择你的味道!

Download: http://jsmooth.sourceforge.net/

2- JarToExe 1.8
Jar2Exe is a tool to convert jar files into exe files.Following are the main features as describe in their website:

2- JarToExe 1.8 Jar2Exe是一个将jar文件转换为exe文件的工具。以下是其网站中描述的主要功能:

  • Can generate “Console”, “Windows GUI”, “Windows Service” three types of exe files.
  • 可以生成“Console”,“Windows GUI”,“Windows Service”三种类型的exe文件。

  • Generated exe files can add program icons and version information.
  • 生成的exe文件可以添加程序图标和版本信息。

  • Generated exe files can encrypt and protect java programs, no temporary files will be generated when program runs.
  • 生成的exe文件可以加密和保护java程序,程序运行时不会生成临时文件。

  • Generated exe files provide system tray icon support.
  • 生成的exe文件提供系统托盘图标支持。

  • Generated exe files provide record system event log support.
  • 生成的exe文件提供记录系统事件日志支持。

  • Generated windows service exe files are able to install/uninstall itself, and support service pause/continue.
  • 生成的Windows服务exe文件能够自行安装/卸载,并支持服务暂停/继续。

  • New release of x64 version, can create 64 bits executives. (May 18, 2008)
  • 新版x64版本,可以创建64位高管。 (2008年5月18日)

  • Both wizard mode and command line mode supported. (May 18, 2008)
  • 支持向导模式和命令行模式。 (2008年5月18日)

Download: http://www.brothersoft.com/jartoexe-75019.html

3- Executor
Package your Java application as a jar, and Executor will turn the jar into a Windows exe file, indistinguishable from a native application. Simply double-clicking the exe file will invoke the Java Runtime Environment and launch your application.

3-执行程序将Java应用程序打包为jar,Executor将jar转换为Windows exe文件,与本机应用程序无法区分。只需双击exe文件即可调用Java Runtime Environment并启动您的应用程序。

Download: http://mpowers.net/executor/

EDIT: The above link is broken, but here is the page (with working download) from the Internet Archive. http://web.archive.org/web/20090316092154/http://mpowers.net/executor/

编辑:以上链接已断开,但这是来自Internet Archive的页面(有工作下载)。 http://web.archive.org/web/20090316092154/http://mpowers.net/executor/

4- Advanced Installer
Advanced Installer lets you create Windows MSI installs in minutes. This also has Windows Vista support and also helps to create MSI packages in other languages.
Download: http://www.advancedinstaller.com/Let me know other tools that you have used to convert JAR to EXE.

4- Advanced Installer Advanced Installer允许您在几分钟内创建Windows MSI安装。这也支持Windows Vista,也有助于使用其他语言创建MSI包。下载:http://www.advancedinstaller.com/告诉我您用于将JAR转换为EXE的其他工具。

#2


31  

I would use GCJ (GNU Compiler for Java) in your situation. It's an AOT (ahead of time) compiler for Java, much like GCC is for C. Instead of interpreting code, or generating intermediate java code to be run at a later time by the Java VM, it generates machine code.

我会在你的情况下使用GCJ(GNU Compiler for Java)。它是用于Java的AOT(提前)编译器,就像GCC用于C而不是解释代码,或者生成中间Java代码以便稍后由Java VM运行,它生成机器代码。

GCJ is available on almost any Linux system through its respective package manager (if available). After installation, the GCJ compiler should be added to the path so that it can be invoked through the terminal. If you're using Windows, you can download and install GCJ through Cygwin or MinGW.

GCJ几乎可以通过其各自的包管理器(如果可用)在任何Linux系统上使用。安装后,应将GCJ编译器添加到路径中,以便可以通过终端调用它。如果您使用的是Windows,则可以通过Cygwin或MinGW下载并安装GCJ。

I would strongly recommend, however, that you rewrite your source for another language that is meant to be compiled, such as C++. Java is meant to be a portable, interpreted language. Compiling it to machine code is completely against what the language was developed for.

但是,我强烈建议您重写另一种要编译的语言的源代码,例如C ++。 Java旨在成为一种可移植的解释语言。将其编译为机器代码完全违背了为其开发的语言。

#3


17  

I use launch4j

我用的是launch4j

ANT Command:

<target name="jar" depends="compile, buildDLLs, copy">    <jar basedir="${java.bin.dir}" destfile="${build.dir}/Project.jar" manifest="META-INF/MANIFEST.MF" /></target><target name="exe" depends="jar">    <exec executable="cmd" dir="${launch4j.home}">        <arg line="/c launch4jc.exe ${basedir}/${launch4j.dir}/L4J_ProjectConfig.xml" />    </exec></target>

#4


3  

I usually use a bat script for that. Here's what I typically use:

我通常使用蝙蝠脚本。这是我通常使用的:

@echo offset d=%~dp0java -Xmx400m -cp "%d%myapp.jar;%d%libs/mylib.jar" my.main.Class %*

The %~dp0 extract the directory where the .bat is located. This allows the bat to find the locations of the jars without requiring any special environment variables nor the setting of the PATH variable.

%~dp0提取.bat所在的目录。这允许蝙蝠找到罐子的位置,而不需要任何特殊的环境变量,也不需要设置PATH变量。

EDIT: Added quotes to the classpath. Otherwise, as Joey said, "fun things can happen with spaces"

编辑:在类路径中添加引号。否则,正如乔伊所说,“空间可能会发生有趣的事情”

#5


2  

We have found Jsmooth to be well-working and easily scriptable with ant under Linux. You may want to use one-jar (also easily scriptable with ant under Linux) to collect a multifile application in a single jar first.

我们发现Jsmooth在Linux下运行良好且易于使用ant编写脚本。您可能希望首先在单个jar中使用one-jar(也可以在Linux下轻松编写脚本)来收集多文件应用程序。

We primarily needed the easy deployment of the EXE combined with the "hey, you need Java version X, go here to download" facilities.

我们主要需要轻松部署EXE并结合“嘿,你需要Java版本X,去这里下载”设施。

(but what you most likely need is the "Runnable jar" / "Executable jar" facility in standard Java).

(但你最需要的是标准Java中的“Runnable jar”/“Executable jar”工具)。

#6


1  

The thing you can do is create a .bat that will execute the .jar file created, checking if there is a JRE present.

您可以做的是创建一个.bat,它将执行创建的.jar文件,检查是否存在JRE。

From Mitch useful link (source)

来自Mitch的有用链接(来源)

java -classpath myprogram.jar de.vogella.eclipse.ide.first.MyFirstClass

This can be used into your batch...

这可以用于您的批次...

#7


0  

There is a small handful of programs that do that... TowerJ is one that comes to mind (I'll let you Google for it) but it costs significant money.

有一小部分程序可以做到这一点... TowerJ是一个浮现在脑海中的想法(我会让你谷歌),但它需要花费很多钱。

The most useful reference for this topic I found is at: http://mindprod.com/jgloss/nativecompiler.html

我找到的这个主题最有用的参考是:http://mindprod.com/jgloss/nativecompiler.html

it mentions a few other products, and alternatives to achieve the same purpose.

它提到了一些其他产品,以及实现相同目的的替代品。