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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2011664/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:30:17  来源:igfitidea点击:

Compiling a java program into an executable

javaexecutablecompilation

提问by Meir

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.

我刚刚用 Eclipse 制作了一个简单的程序,我想将它编译成一个可执行文件,但似乎根本无法找到如何去做。

采纳答案by Sajad Bahmani

You can convert .jar file to .exe on these ways:
alt text
(source: viralpatel.net)

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:(来源:viruspatel.net1- 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/

下载: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.
  • Generated exe files can add program icons and version information.
  • Generated exe files can encrypt and protect java programs, no temporary files will be generated when program runs.
  • Generated exe files provide system tray icon support.
  • Generated exe files provide record system event log support.
  • Generated windows service exe files are able to install/uninstall itself, and support service pause/continue.
  • New release of x64 version, can create 64 bits executives. (May 18, 2008)
  • Both wizard mode and command line mode supported. (May 18, 2008)
  • 可以生成“Console”、“Windows GUI”、“Windows Service”三种类型的exe文件。
  • 生成的exe文件可以添加程序图标和版本信息。
  • 生成的exe文件可以对java程序进行加密和保护,程序运行时不会产生临时文件。
  • 生成的 exe 文件提供系统托盘图标支持。
  • 生成的 exe 文件提供记录系统事件日志支持。
  • 生成的windows服务exe文件可以自行安装/卸载,支持服务暂停/继续。
  • 新发布的 x64 版本,可以创建 64 位执行程序。(2008 年 5 月 18 日)
  • 支持向导模式和命令行模式。(2008 年 5 月 18 日)

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

下载: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- Executor
将您的Java 应用程序打包为jar,Executor 会将jar 转换为Windows exe 文件,与本机应用程序无法区分。只需双击 exe 文件即可调用 Java 运行时环境并启动您的应用程序。

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

下载: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- 高级安装程序
高级安装程序可让您在几分钟内创建 Windows MSI 安装。这也有 Windows Vista 支持,还有助于创建其他语言的 MSI 包。
下载:http: //www.advancedinstaller.com/让我知道您用于将 JAR 转换为 EXE 的其他工具。

回答by Vincent B.

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)

来自米奇有用的链接(来源

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

This can be used into your batch...

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

回答by Carl Smotricz

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.

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

回答by Itay Maman

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

我通常为此使用 bat 脚本。这是我通常使用的:

@echo off
set d=%~dp0
java -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 所在的目录。这允许 bat 找到 jars 的位置,而不需要任何特殊的环境变量,也不需要设置 PATH 变量。

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

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

回答by jakogut

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 旨在成为一种可移植的解释型语言。将其编译为机器代码完全违背了该语言的开发目的。

回答by r3zn1k

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>

回答by Thorbj?rn Ravn Andersen

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 脚本。您可能希望首先使用 one-jar(在 Linux 下也可以使用 ant 轻松编写脚本)在单个 jar 中收集多文件应用程序。

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”工具)。