如何用java创建exe文件
您好,我想为我的 java 应用程序创建一个 exe 文件。
我尝试使用一些第三方软件JEXECreator,成功创建了exe文件并且它在我的系统中工作正常,当我尝试使用另一台机器时,它不起作用。我收到以下错误
* The error occurred while running the application. The exit code is 0x10000223.
* Contact the vendor of the application for troubleshooting.
java.lang.ClassNotFoundException: com.sample.SampleMain
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.ucware.JEXEClassLoader.run(Unknown Source)
at com.ucware.JEXEClassLoader.main(Unknown Source)
**************************************
我知道我设置的类路径有问题。
实际上我想自己创建exe文件而不使用任何第三方软件。
我在很多站点中找到了这些步骤
创建了名为 Sample.mft 的清单文件,其中包含以下内容
Manifest-Version: 1.0
Main-Class:
Class-path:
在此我有一些疑问,
应如何添加主类以及完整的包名称(com.sample.SampleMain)或单独的类名称 (SampleMain) 或带有扩展名 (SampleMain.class)
如何添加类路径,我的项目中有 4 个 java 类和 2 个 jar。如何在类路径中添加所有这些,以及我是否需要在类路径中添加java jdk。
清单文件的保存位置
清单文件扩展名应该是什么(mf 或 mft)
在命令提示符下,我应该从哪个目录创建 exe 文件(从我的项目文件夹或 src 文件夹或包含所有 java 类的文件夹)
在命令提示符中创建 jar 时应使用什么语法
(jar cmf Sample.mf Sample.jar Sample1.class Sample2.class Sample3.class Sample4.class jar1.jar jar2.jar) 像这样或 (jar cvfm Sample.jar example.mf *.class)
当我做了这样的事情时我得到的是一个 jar 而不是 exe 文件,当我使用“java -jar example.jar”在命令提示符中运行 jar 时,我得到了“类未找到异常”。
实际上,如何创建 exe 文件而不是 jar 文件,这意味着只需双击该 exe 文件,就可以在任何计算机上运行我的应用程序。
谁能帮我做到这一点?
提前致谢。
Hi I want to create an exe file for my java app.
I tried with some third party softwares JEXECreator, successfully created the exe file and its working fine in my system, when I tried with another machine, it’s not working. I got the following error
* The error occurred while running the application. The exit code is 0x10000223.
* Contact the vendor of the application for troubleshooting.
java.lang.ClassNotFoundException: com.sample.SampleMain
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.ucware.JEXEClassLoader.run(Unknown Source)
at com.ucware.JEXEClassLoader.main(Unknown Source)
**************************************
I know there is something wrong with the classpath which I set.
Actually I want to create the exe file myself without using any third party software.
I found the steps in lot of sites
Created the manifest file named Sample.mft with following contents
Manifest-Version: 1.0
Main-Class:
Class-path:
In this I have some doubts,
How the Main-Class should be added, with the full package name (com.sample.SampleMain) or the class name alone (SampleMain) or with the extension (SampleMain.class)
How the class-path should be added, I have 4 java classes and 2 jars in my project. How to add all these in the class path, and do I need to add the java jdk in classpath.
Where the manifest file should be saved
What should be the manifest file extension (mf or mft)
In command prompt from which directory I should create the exe file (from my project folder or src folder or the folder which contains all the java classes)
What’s the syntax should be used while creating jar in command prompt
(jar cmf Sample.mf Sample.jar Sample1.class Sample2.class Sample3.class Sample4.class jar1.jar jar2.jar) like this or (jar cvfm Sample.jar sample.mf *.class)
When I did something like this I am getting a jar instead of exe file, When I run the jar in command prompt using "java -jar sample.jar" then I am getting class not found exception".
Actually how to create an exe file instead of jar file, that means just by double clicking that exe file, should run my app in any machine.
Can anyone help me to do this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不能代表 JEXECreator,但我可以推荐 JSmooth (http://jsmooth.sourceforge.net/ )。我已经成功地将它用于多个项目(例如这个基于SWT的Java应用程序)。
Can't speak for JEXECreator, but I can recommend JSmooth (http://jsmooth.sourceforge.net/). I've successfully used it for several projects (e.g. this SWT based Java app).
就我个人而言,我喜欢 JSmooth。那只是包装。这意味着它仍然是一个 Java 应用程序。执行 exe 时,它将把 jar 解压到临时文件夹,然后使用
javaw -jar ...
执行它。第二个选项是 gcj.但这绝对是一个糟糕的选择。这不会将 jar 包装在 exe 中,但它确实将其编译为本机系统代码。但这会非常减慢您的应用程序的速度。您可以在本主题上查看我的一些计时结果。
Personally, I like JSmooth. That is just wrapping. This means it is still a Java application. When executing the exe, it will unpack the jar to a temporary folder and then execute it with
javaw -jar ...
A second option is gcj. But that is absolutely a bad choice. That doesn't wrap the jar in an exe, but it really compiles it to native system code. But this slows down your application very much. You can check some results of my timing on this topic.
我在 Excelsior JET 方面获得了一些积极的体验。与 gcj 不同,它实际上可以工作,并且执行时间比执行的 .jar 文件更快。缺点是它不是免费的。
I had some positive experience with Excelsior JET. Unlike gcj it actually works and execution times are faster than that of an executed .jar file. The downside is that it's not for free.
好吧,我建议您创建一个 bash 文件,而不是做复杂的事情,然后双击它就可以运行您的应用程序。是的,但您无法更改其图标,但有许多免费工具可用,您可以使用它们轻松转换 bash文件到exe。
Well I will recommend you to create one bash file instead of doing complex things and by double clicking on it you can run your application.Yeah but you cant change its icon but there are many free tools are available by using which you can easily convert bash file to exe.
为了创建 exe 文件,我使用 launch4j。 Launch4j 将 jar 文件转换为 exe 文件,如果我想将其打包用于安装程序,我使用 InnoSetup。对我来说,由 launch4j 创建的 exe 可在 PC 上运行。
To create an exe file, I use launch4j. Launch4j converts the jar file into exe file and if I wanna pack it for the installer, I use InnoSetup. For me, that exe created by launch4j works on pcs.
我使用 Eclipse IDE 下的 Ant 工具与 InnoSetup 和 Launch4J 一起创建 EXE 及其安装程序,它还管理类路径...
指南?您可以参考:
http://www.eteks.com/tips/tipCreationExe.html(法语)
I use the Ant tool under Eclipse IDE to work with InnoSetup and Launch4J to create the EXE and its installer which it also manages the classpath...
A guide? You can refer to:
http://www.eteks.com/tips/tipCreationExe.html (in French)